Problem with set(gca, 'Position', [...]);

121 次查看(过去 30 天)
Hi there,
I've recently moved from v2013a Matlab to v2020a (an overdue change driven by my institute). I've got a plotting script that I was working on at v2013a just the other day but which now behaves strangely at v2020a.
It has two subplots, and right at the end, repositions the second to match the first's horizonal position.
figure(1); clf;
subplot (2,1,1);
pcolor (tval, yval, t1); shading flat;
ylabel (ystr); title (titstr1);
cb = colorbar; pos1 = get(gca, 'Position');
subplot (2,1,2);
plot (tval, t2, 'k-');
ylabel (ystr);
xlabel ('Time [year]'); title (titstr2);
pos2 = get(gca, 'Position'); pos2(1) = pos1(1); pos2(3) = pos1(3); set (gca, 'Position', pos2);
What should happen is that the second subplot's horizontal length is shortened to match the first subplot's reduced length (which stems from it having a colorbar). I do this so that the two time-series are aligned on the plot.
And this script works fine at v2013a. But at v2020a, when I cut and paste the above into the command window (my preferred mode of working), it does everything except for the repositioning. Even though the command is right there. Even more strangely, if I then manually paste the final statement ...
set (gca, 'Position', pos2);
... into the command window after it's failed to do what I ask, it then does it correctly. I can think of no good reason why this is, and my attempts to "trick" Matlab (e.g. by duplicating the set statement, or by assigning a named handle to the subplot) have all failed. It needs me to separately paste in the final command for it to properly execute it. Which is completely impractical for this particular script because the plotting is meant to be part of a loop over many plots.
Over the (many) years I've used Matlab, I'm familiar with the ordering of commands being important to the final result, but this failure to do what it's told, and then doing what it's told (but forcing me to ask it in a clunky manual fashion) is new to me.
What am I missing about how v2020a works?
Best regards,
Andrew.

采纳的回答

Walter Roberson
Walter Roberson 2020-8-21
Axes position is not calculated until the figure is rendered. You should add a drawnow() before getting the axes position.
  2 个评论
Andrew Yool
Andrew Yool 2020-8-21
Bingo! Thanks very much Walter - that does the trick. I've actually come across drawnow in the past, but I've almost never had to use it (and certainly not in v2013a). Anyway, it does exactly what I need it to.
For reference, what I needed to do to my script above to get it to work was ...
figure(1); clf;
subplot (2,1,1);
pcolor (tval, yval, t1); shading flat;
ylabel (ystr); title (titstr1);
cb = colorbar; pos1 = get(gca, 'Position');
subplot (2,1,2);
plot (tval, t2, 'k-');
ylabel (ystr);
xlabel ('Time [year]'); title (titstr2);
pos2 = get(gca, 'Position'); pos2(1) = pos1(1); pos2(3) = pos1(3);
drawnow;
set (gca, 'Position', pos2);
Matlab then took just a few extra seconds to plot the figure - I think it was building suspense!
Walter Roberson
Walter Roberson 2020-8-22
As of R2014b, graphics rendering was moved to a different thread, and the information about updates to the data structures is not sent to the graphics thread until drawnow() or pause() or a small number of other activities, including returning to the command line.
Especially when you use the new object notation to set properties, you really do not want to submit everything to rendering immediately. For example it is not uncommon to change the number of entries in xticks and then to update xticklabels, and with the new notation like
ax.XTicks = something
ax.XTickLabels = appropriate
then there is no explicit way to say "postpone updating the graphics, I am making a block of changes during which some of the data properties might be temporarily inconsistent... okay now it is safe to update again". The postpone becomes implicit, properties are permitted to be inconsistent until dranow() or one of the other specific activities takes place.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by