Title appearing on wrong figure

15 visualizzazioni (ultimi 30 giorni)
Jonathan Wittmer
Jonathan Wittmer il 3 Apr 2020
I have a class method that I am using to plot some images. The images are passed in a vectors and reshaped into the image.
classdef MyClass
methods(Static)
function plot_image(x, shape, my_title)
figure
imshow(reshape(x, shape));
title(my_title, 'Fontsize', 16);
end
function plot_two_images(x, y, shape, my_titles)
figure
subplots(1,2,1)
imshow(reshape(x, shape));
title(my_titles(1), 'Fontsize', 16);
subplots(1,2,2)
imshow(reshape(y, shape));
title(my_titles(2), 'Fontsize', 16);
end
end
end
When I try plotting with
my_cls = MyClass
my_cls.plot_two_images(x,y,shape,{'1','2'})
my_cls.plot_image(x,shape,'3')
my_cls.plot_image(y,shape,'4')
the titles show up on the previous image. So, 3 shows up where 2 should be, 4 shows up where 3 should be and the 4th image has no title. Why would this be the case?

Risposta accettata

Tommy
Tommy il 3 Apr 2020
For both imshow() and title(), you can specify a target axes:
f = figure;
ax = axes(f);
imshow(reshape(x, shape), 'Parent', ax);
title(ax, my_title, 'Fontsize', 16);

Più risposte (1)

Jonathan Wittmer
Jonathan Wittmer il 3 Apr 2020
The issue seems to not have anything to do with calling functions or using classes as the problem persists even when putting the plotting code inline with the calling code.
It seems that there is a timing mismatch between when I am calling
figure()
plotting with
imshow()
and setting the title. The issue was resolved by putting pause statements after figure() and after title(). This seems like a silly workaround though... I shouldn't have to put pause statements in the code to make sure it gets executed in the order its called. If anybody has a better way to get this working right, I'd be happy to hear.
  4 Commenti
Tommy
Tommy il 3 Apr 2020
Of course!
To be honest, I'm not sure... imshow(I) without the 'Parent' argument "displays the grayscale image I in a figure" according to the docs. I would assume it looks for gcf and then either finds the current axes in that figure if they exist or creates a new set of axes in that figure if not. title places your title in gca unless you explicitly specify the axes. It might be possible that gcf and gca do not update quickly enough after your calls to figure and imshow, respectively?
Something else to consider: a call to subplot creates a set of axes, meaning the call to imshow here:
figure
subplots(1,2,1)
imshow(reshape(x, shape)); % gcf already has axes
title(my_titles(1), 'Fontsize', 16);
is not quite the same as the call to imshow here:
figure
imshow(reshape(x, shape)); % gcf has no axes (until imshow creates them)
title(my_title, 'Fontsize', 16);
with regards to whether imshow needs to make a new set of axes. This is all just me throwing out ideas though, no real answer...
Dimitris Ampeliotis
Dimitris Ampeliotis il 22 Lug 2021
I had the exact same issue when drawing in subsequent figures. It seems that placing a drawnow statement before the commands that draw on the next figure, resolves the issue.

Accedi per commentare.

Categorie

Scopri di più su Creating, Deleting, and Querying Graphics Objects in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by