Title appearing on wrong figure
15 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Jonathan Wittmer
il 3 Apr 2020
Commentato: Dimitris Ampeliotis
il 22 Lug 2021
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?
0 Commenti
Risposta accettata
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);
0 Commenti
Più risposte (1)
Jonathan Wittmer
il 3 Apr 2020
4 Commenti
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
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.
Vedere anche
Categorie
Scopri di più su Creating, Deleting, and Querying Graphics Objects in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!