Title appearing on wrong figure
18 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jonathan Wittmer
el 3 de Abr. de 2020
Comentada: Dimitris Ampeliotis
el 22 de Jul. de 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 comentarios
Respuesta aceptada
Tommy
el 3 de Abr. de 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 comentarios
Más respuestas (1)
Jonathan Wittmer
el 3 de Abr. de 2020
4 comentarios
Tommy
el 3 de Abr. de 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
el 22 de Jul. de 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.
Ver también
Categorías
Más información sobre Creating, Deleting, and Querying Graphics Objects en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!