简体   繁体   English

如何确定Matlab中子图的尺寸?

[英]How to determine the dimensions of a subplot in Matlab?

So I am writing a function that plots matrix data from n different cells.所以我正在编写一个函数来绘制来自n 个不同单元格的矩阵数据。 If n is 10, it should display 10 equally spaced plots on a single figure.如果n为 10,它应该在单个图形上显示 10 个等距图。 If n is 7, it should try to space them out as equally as possible (so 3x2 or 2x3 plots with a plot by itself).如果n是 7,它应该尽量将它们间隔开(所以 3x2 或 2x3 绘图本身有一个绘图)。

I am able to get these graphs drawn using subplot() and plot() but I'm having a hard time finding out how to initialise the dimensions for the subplot.我能够使用subplot()plot()绘制这些图形,但是我很难找到如何初始化子图的尺寸。

The number of subplots will be changing after each run so I can't initialise it to specific dimensions.每次运行后子图的数量都会发生变化,因此我无法将其初始化为特定尺寸。

Can anyone point me in the right direction?任何人都可以指出我正确的方向吗?

I am afraid problems like this tend to be messy.恐怕像这样的问题往往很混乱。 This normally problems like this need to be solved for different cases.这通常需要针对不同的情况解决这样的问题。

if (mod(n,2) && n<8)
    % Do something
elseif (!mod(n,2) && n < 11)
    % Do something else
elseif ...
    ....
end

The conditions are choosen a bit arbitarily since the specifications in the OP seemed a bit arbitary too.条件的选择有点武断,因为 OP 中的规范似乎也有点武断。 You probably understand the point and can set your own conditions.您可能理解这一点,并且可以设置自己的条件。

There are two reasons why I recommend this approach.我推荐这种方法有两个原因。

1) This makes the code simpler to write. 1) 这使得代码更易于编写。 You do not have to come up with some complicated solution which may break in after some time.您不必想出一些复杂的解决方案,这些解决方案可能会在一段时间后失效。

2) By adding cases you can protect yourself against a rampant number of plots. 2)通过添加案例,您可以保护自己免受大量阴谋的影响。 In case the number of plots gets too large you do typically not want to have all plots in the same figure.如果图的数量太大,您通常不希望所有图都在同一个图中。 It is also possible to wrap this into a function and apply this to X plots at a time in a loop.也可以将其包装到一个函数中,并在循环中一次将其应用于 X 绘图。 Typically you would want each iteration to be a separate figure.通常,您希望每次迭代都是一个单独的图形。

It is not very easy to elaborate more on this since you have not yet specified how many cases you expect or what will happen to the last plot in case of odd numbers.对此进行详细说明并不容易,因为您尚未指定预期的案例数量或奇数情况下最后一个情节会发生什么。 Still this may give a good hint.尽管如此,这可能会给出一个很好的提示。

Good luck!祝你好运!

Another simple solution would be using round and ceil on the square root:另一个简单的解决方案是在平方根上使用roundceil

for n=1:20
    [n, round(sqrt(n))*ceil(sqrt(n)),  round(sqrt(n)), ceil(sqrt(n))]
end

output:输出:

%(n, total_plots, x, y)
 1     1     1     1
 2     2     1     2
 3     4     2     2
 4     4     2     2
 5     6     2     3
 6     6     2     3
 7     9     3     3
 8     9     3     3
 9     9     3     3
10    12     3     4

Usage example:用法示例:

n = 7
subplot(round(sqrt(n)), ceil(sqrt(n)), plot_nr_x) % switch first 2 params to have either a slightly longer or slightly wider subplot

I ran into a very similar problem today and I was having a lot of trouble to define the size of the subplot that would fit everything.我今天遇到了一个非常相似的问题,我在定义适合所有内容的子图的大小时遇到​​了很多麻烦。 My reasoning is mostly a hack but it can help.我的推理主要是一个黑客,但它可以提供帮助。 If you have to represent at most n figures, you can thing as a square grid of sqrt(n) * sqrt(n) .如果您最多必须表示n数字,则可以将其视为sqrt(n) * sqrt(n)的方形网格。 To make things better we add a safety row, so the final matrix would be (sqrt(n) + 1) * sqrt(n) .为了让事情变得更好,我们添加了一个安全行,因此最终矩阵将是(sqrt(n) + 1) * sqrt(n) I hope this helps solving your problem.我希望这有助于解决您的问题。

In my code have 2 nested loops:在我的代码中有 2 个嵌套循环:

within a loop that opens a figure for each kk element and is meant to plot a particular graph from the x position within the array.在一个循环中,为每个kk元素打开一个图形,并打算从数组中的x位置绘制一个特定的图形。

for kk=1:length(some_file_list)

      % Load data
      % do some math
      % get data as a cell array with things we care about in data(3,)

    array_size = size(data(3,:),2);

      for x=1:size(data(3,:),2);

    % do more math and get things ready to plot matrix_A scaled by range_A

            figure(kk); % open figure       
                grid_rows = round((sqrt(array_size)+1));
                grid_cols = round(sqrt(array_size));

                % plot
                subplot(grid_rows, grid_cols, x);
                imagesc(matrix_A,range_A); %plot in position
                colormap(gray);
         end
 end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM