MATLAB - 如何将子图缩放在一起?

Mie*_*ter 48 matlab plot zoom

我在一个图中有多个子图.每个图的X轴是相同的变量(时间).每个图上的Y轴是不同的(它代表什么和数据的大小).

我想要一种方法同时放大所有绘图的时间尺度.理想情况下,通过在其中一个图上使用矩形缩放工具,并使其他图相应地更改其X限制.对于所有这些,Y限制应该保持不变.自动拟合数据以在Y方向上填充图是可以接受的.

(这个问题几乎与Stack Overflow问题一相同Matplotlib/Pyplot:如何将子图缩放在一起?(MATLAB除外))

Yai*_*man 45

使用内置的linkaxes功能如下:

linkaxes([hAxes1,hAxes2,hAxes3], 'x');
Run Code Online (Sandbox Code Playgroud)

对于更高级的链接(不仅仅是x或y轴),请使用内置的linkprop函数


YYC*_*YYC 29

使用linkaxesYair和Amro已经建议.以下是您案例的快速示例

ha(1) = subplot(2,1,1); % get the axes handle when you create the subplot
plot([1:10]);           % Plot random stuff here as an example
ha(2) = subplot(2,1,2); % get the axes handle when you create the subplot
plot([1:10]+10);        % Plot random stuff here as an example

linkaxes(ha, 'x');      % Link all axes in x
Run Code Online (Sandbox Code Playgroud)

您应该能够同时放大所有子图

如果有许多子图,并且逐个收集它们的轴手柄似乎不是一个聪明的方法来完成这项工作,你可以通过以下命令找到给定图形句柄中的所有轴处理

figure_handle = figure;
subplot(2,1,1); 
plot([1:10]);   
subplot(2,1,2); 
plot([1:10]+10);

% find all axes handle of type 'axes' and empty tag
all_ha = findobj( figure_handle, 'type', 'axes', 'tag', '' );
linkaxes( all_ha, 'x' );
Run Code Online (Sandbox Code Playgroud)

第一行查找figure_handle"axes"类型下的所有对象和空标记('').空标记的条件是排除标记的斧柄,其标记将是legend.

如果它不仅仅是一个简单的图,那么你的图中可能还有其他轴对象.在这种情况下,您需要添加更多条件来标识您感兴趣的图的轴控制柄.