Matlab 对于图中的多个子批次,分别使所有右侧和左侧y轴限制相同

Matlab 对于图中的多个子批次,分别使所有右侧和左侧y轴限制相同,matlab,matlab-figure,handle,subplot,axes,Matlab,Matlab Figure,Handle,Subplot,Axes,我有一个子图,如下所示: fig = figure; nPlots = 3; for p = 1:nPlots ax(p) = subplot(1, nPlots, p); x = 0:10*p; y1 = max(x)-x.^1.1; y2 = p*max(x)-2*x; yyaxis left; plot(x, y1); yyaxis right; plot(x, y2); end 其创建如下所示: fig = figure; nPlots = 3; f

我有一个子图,如下所示:

fig = figure;
nPlots = 3;
for p = 1:nPlots
  ax(p) = subplot(1, nPlots, p);
  x  = 0:10*p;
  y1 = max(x)-x.^1.1;
  y2 = p*max(x)-2*x;
  yyaxis left;
  plot(x, y1);
  yyaxis right;
  plot(x, y2);
end

其创建如下所示:

fig = figure;
nPlots = 3;
for p = 1:nPlots
  ax(p) = subplot(1, nPlots, p);
  x  = 0:10*p;
  y1 = max(x)-x.^1.1;
  y2 = p*max(x)-2*x;
  yyaxis left;
  plot(x, y1);
  yyaxis right;
  plot(x, y2);
end
我希望每一侧的轴分别相同,即我希望所有绘图都具有:

ylim_left  = [0 30];
ylim_right = [0 90];
但是如果我使用
linkaxes(ax)
我最终只得到了要更改的右侧:

如果我尝试
yy左轴;连接轴(ax)
然后取左侧的最大值,并将其应用于前两个子批次的右侧


当我检查ax时,我注意到它的所有组件ax(1)、ax(2)。。。将
YAxisLocation:
属性设置为
'right'
,我认为这是问题的根源。我不确定如何将手柄直接连接到子地块的左右两侧轴,从而将它们直接连接在一起。任何想法都将不胜感激。

只需在绘图后插入“
ylim
”函数即可。这对我很有用:

fig = figure;
nPlots = 3;
ylim_left  = [0 30];
ylim_right = [0 90];
for p = 1:nPlots
  ax(p) = subplot(1, nPlots, p);
  x  = 0:10*p;
  y1 = max(x)-x.^1.1;
  y2 = p*max(x)-2*x;
  yyaxis left;
  plot(x, y1);
  ylim(ylim_left);
  yyaxis right;
  plot(x, y2);
  ylim(ylim_right);
end

提取Y轴的
NumericRuler
子对象可访问左右限制:

>> [ax.YAxis]

ans = 

  2×3 NumericRuler array:

    NumericRuler    NumericRuler    NumericRuler
    NumericRuler    NumericRuler    NumericRuler
其中第1行为左侧,第2行为右侧

定义一个帮助器函数以同步以下内容:

function matchyyaxes(ax, ylim_L, ylim_R)
yaxes = [ax.YAxis];

set(yaxes(1,:), 'Limits', ylim_L);
set(yaxes(2,:), 'Limits', ylim_R);
end
您可以将其与样本限值一起使用:

matchyyaxes(ax, [0 30], [0 90]);



您也可以创建自己的,以模仿

的功能,感谢您的响应。如果我已经知道最大值,这会起作用。在每种情况下我都不需要,所以我想在所有绘图都创建完成后,将最大范围应用于所有绘图。好的,那么您应该首先进行计算并保留结果。然后使用循环进行绘图。