How do I use tiledlayout when each plot is made within a subroutine?

13 次查看(过去 30 天)
Most Documentation and community Answers related to tilelayout use plots formed by simple calls to MATLAB plotting routines, similar to this excerpt from the tiledlayout Documentation page:
tiledlayout(2,2);
[X,Y,Z] = peaks(20);
% Tile 1
nexttile
surf(X,Y,Z)
% Tile 2
nexttile
contour(X,Y,Z)
% ...
I want to tile several plots, each of which is made by one call to a subroutine. (Why is each made within a subroutine? because it takes tens of lines of code to accomplish what I want. See https://www.mathworks.com/matlabcentral/answers/699755-fancy-correlation-plots-in-matlab#answer_582307 for the roots of my subroutine.)
How can I use the plots created by several calls to a subroutine with tiledlayout? What exactly needs to be assigned to a variable within the subroutine and passed to the calling program? In each of the plots I want to preserve the sizes of individual points in a scatterplot, the custom colormap (the same one is used for all plots), the size and position of text, etc.
  3 个评论
Leslie
Leslie 2022-3-26
Nice idea, thanks! (And yes, it is a function not a subroutine.)
Your suggestion did enable the tiling, but for each subplot the following chunk of code from the plotting function (which is attached)
% Use the same length for the data units along each axis of the plot.
h.Visible = 'off';
disp("original h.Position");h.Position
h.Position(4) = h.Position(4)*0.9;
disp("after resize h.Position(4)");h.Position(4)
axis(h, 'equal') % inner x=0.1191,y=0.11,wid=0.71,ht=0.7335
disp(" h.Position after set h equal");h.Position
yielded the following error message amongst the screen output:
Warning: Unable to set 'Position', 'InnerPosition', 'OuterPosition', or 'ActivePositionProperty' for objects in a TiledChartLayout
The overall result is pretty ugly:
For reference, here's what a standalone plot created by this function looks like:
Leslie
Leslie 2022-3-26
To answer your question about what went wrong when I was calling nexttile() before each call to the plotting function -- things happened fast, but I appeared to get a figure prepared for tiling and then a new figure that had the actual plot in it.

请先登录,再进行评论。

回答(1 个)

Dave B
Dave B 2022-3-26
You're seeing the error because when tiledlayout is in charge of positioning axes, it doesn't let you do it.
It looks to me like there's some code that's specifically setting out to set the position, but it goes against what tiledlayout is trying to do...tiledlayout is really all about trying to keep your various axes aligned. And it does this whenever your resize or change the labels or do other things that affect their position. So if you could change position yourself these two concepts would be at odds.
You're seeing the strange looking plot because there's nothing that adjusts the size of the markers based on the size of the axes. You could manually make your markers smaller looking for a size that worked well, or add (yet another) parameter to your function.
I think the short answer is:
  • remove the code setting position
  • change the size of the markers to make them smaller
  • maybe the same for the fontsize?
  • (or just make the figure window much bigger)
I experimented (very briefly) by:
Messing around with arguments till it plotted something, taking the nexttile out of the function and calling it from the outside, making the markers half as big, resizing all of the axes to have the same limits. I ended up with this (I didn't work on giving it real data or getting the colorbar to show up or messing with the text):
Longer answer:
Charts like this are part of why we built a framework for authoring charts. There's a bit of a learning curve, and I'm not sure it'll solve all of your problems, but the idea was to wrap up a bunch of parameters that determine a chart into an object...similar in some ways to some of our built-in MATLAB charts. I'm not sure it'll be worth it for you, but if you're interested you can see the doc here: https://www.mathworks.com/help/matlab/developing-chart-classes.html and some examples here: https://www.mathworks.com/matlabcentral/fileexchange?q=tag%3Achartcontainer
The idea is that you end up with an object that can take name value pairs, and that can have parameters changes after you create it. In your case an advantage would be that you could adjust the size of circles (for instance) after having all of your tiles displayed, rather than before.
Like I said, there's a learning curve, so I'm not sure it'll be worth it to you.

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by