更改子图的 ylim

Changing ylim's of subplotted graphs

我使用命令 subplot(1,2,1)subplot(1,2,2) 绘制了图表。我绘制了数据。如您所见,我在图中有 2 个图表。

我想更改两个图表的 y 轴边界。

例如,第一张图为 ylim=[0 0.5],第二张图为 ylim=[0 1.5]。我想改变这些 ylim 的。

首先您需要访问您的人物:

Myfig=gcf; %If the figure you want to modify is the current figure
Myfig=figure(1); %Replace 1 by the number of your figure, this command will make figure(1) your current figure and return its handle in Myfig.

现在您的子图存储在图的 'Children' 字段中。在这种情况下,它将是一个元胞数组,其中包含两个表示子图的坐标区对象。

%If you don't have legends
Allsubplots=get(Myfig,'children'); 

% If you have legends, they will also appear as children of your fig
% Meaning you have to seek all fig's children that are of type 'axes'
Allsubplots=findobj(Myfig,'type','axes');

MySubplot1=Allsubplots(1);
MySubplot2=Allsubplots(2);

%%Change ylims of first subplot to [Ymin1 Ymax1] :
set(MySubplot1,'ylim',[Ymin1 Ymax1]);

%%Same for second subplot :
set(MySubplot2,'ylim',[Ymin2 Ymax2]);

更新:如果您已经先验知道您想要更改哪个子图以及如何AND 你要修改的图就是你现在的图:

MySubplot1=subplot(1,2,1);
MySubplot2=subplot(1,2,2);

%%Change ylims of first subplot to [Ymin1 Ymax1] :
set(MySubplot1,'ylim',[Ymin1 Ymax1]);

%%Same for second subplot :
set(MySubplot2,'ylim',[Ymin2 Ymax2]);