设置轴最小值和最大值

时间:2022-10-24 20:11:25

I have a (3,4) subplot each showing scatterplots. The ranges of the scatterplots vary so some of my plots have axes x(0-30) and y(0-8), but some have x(18-22) and y(4-7). I have set my xlim to [0 30], and ylim to [0 8] but that sets my axes to never go lower than 0, higher than 30 etc.

我有一个(3,4)子图每个都显示散点。散点图的范围是不同的,所以我的一些图中有坐标轴x(0-30)和y(0-8),但是有些图中有x(18-22)和y(4-7)。我将xlim设为[0 30],ylim设为[0 8],但这将使坐标轴永不低于0、高于30等等。

How do I set my axis to "stick" at (0,0) for the origin of each plot, and "stick" at 8 for Y and 30 for X.

如何设置坐标轴在(0,0)处表示每幅图的原点,在(8,0)处表示Y,在(30)处表示X。

TIA for any help

TIA的任何帮助


update per comment on answer:
Still having the same issue with below code

更新每个评论的答案:仍然有同样的问题与下面的代码

%% plot

for i = 1:num_bins;

h = zeros(ceil(num_bins),1);

h(i)=subplot(4,3,i);

plotmatrix(current_rpm,current_torque)

end

linkaxes(h,'xy');

axis([0 30 0 8]);

1 个解决方案

#1


7  

To programmatically set axis boundaries there are a few useful commands:

要以编程方式设置轴的边界,有一些有用的命令:

axis([0 30 0 8]);  %Sets all four axis bounds

or

xlim([0 30]);  %Sets x axis limits
ylim([0 8]);   %Sets y axis limits

To only set one of the two x limits I usually use code like this:

为了只设置两个x限制中的一个,我通常使用如下代码:

xlim([0 max(xlim)]);  %Leaves upper x limit unchanged, sets lower x limit to 0

This takes advantage of xlims zero input argument calling convention, which returns an array of the current x limits. The same works with ylim.

这利用了xlims zero输入参数调用约定,它返回当前x限制的数组。ylim也是如此。

Note that all of these commands apply to the current axis, so if you are creating subplots you will need to perform the scaling call once per axis as you build up your figure.

请注意,所有这些命令都适用于当前轴,因此,如果您正在创建子图,那么在您构建图形时,您将需要在每个轴上执行缩放调用。


Another useful fatures is the linkaxes command. This dynamically links the axis limits of two plots, including for programmatic resize commands like xlim and UI operations like pan and zoom. For example:

另一个有用的方法是linkaxes命令。这将动态地链接两个图的轴限值,包括xlim等编程调整大小命令和pan和zoom等UI操作。例如:

a(1) = subplot(211),plot(rand(10,1), rand(10,1)); %Store axis handles in "a" vector
a(2) = subplot(212),plot(rand(10,1), rand(10,1)): %

linkaxes(a, 'xy');

axis([0 30 0 8]);  %Note that all axes are now adjusted together
%Also try some manual zoom, pan operations using the UI buttons.

Looking at your code, post edit, your use of the plotmatrix function is complicating things. plotmatrix appears to create its own axes to work in, so you need to capture those handles and adjust them. (Also, in the future take h = zeros(..) out of the loop).

看看你的代码,发布编辑,你对plotmatrix函数的使用让事情变得复杂。plotmatrix似乎创建了自己的轴来工作,因此需要捕获这些句柄并对它们进行调整。(同样,在未来,h = zeros(..)退出循环)。

To get the handles to the plotmatrix created axes, use the second return argument, like this: [~, hAxes]=plotmatrix(current_rpm,current_torque);. Then collect those for future use.

要获取创建的plotmatrix的句柄,请使用第二个返回参数:[~,hAxes]=plotmatrix(current_rpm,current_torque);然后收集它们供将来使用。

Finally, the axis, xlim, ylim commands all act on the current axis, (see gca). However the plotmatrix axes are never current, so the axis command has not been affecting them. You can specify the axis to act on, like this: axis(hAxis, [0 30 0 8]);.

最后,轴、xlim、ylim命令都在当前轴上执行(参见gca)。但是plotmatrix坐标轴从来不是当前的,所以axis命令没有影响它们。可以指定要执行的轴,如下所示:axis(hAxis, [0 30 0 0 8]);

Putting this all together (an adding some variable definitions to get your code to execute), and this is what it looks like:

把所有这些放在一起(添加一些变量定义来执行代码),这就是它的样子:

%Define some dummy variables
current_rpm = rand(20,1)*30;
current_torque = rand(20,1)*8;
num_bins = 12;

%Loop to plot, collecting generated axis handles into "hAllAxes"
hAllAxes = [];
for i = 1:num_bins;
    subplot(4,3,i);
    [~, hCurrentAxes]=plotmatrix(current_rpm,current_torque);
    hAllAxes = [hAllAxes hCurrentAxes];  %#ok
end
linkaxes(hAllAxes,'xy');    
axis(hAllAxes,[0 30 0 8]);

#1


7  

To programmatically set axis boundaries there are a few useful commands:

要以编程方式设置轴的边界,有一些有用的命令:

axis([0 30 0 8]);  %Sets all four axis bounds

or

xlim([0 30]);  %Sets x axis limits
ylim([0 8]);   %Sets y axis limits

To only set one of the two x limits I usually use code like this:

为了只设置两个x限制中的一个,我通常使用如下代码:

xlim([0 max(xlim)]);  %Leaves upper x limit unchanged, sets lower x limit to 0

This takes advantage of xlims zero input argument calling convention, which returns an array of the current x limits. The same works with ylim.

这利用了xlims zero输入参数调用约定,它返回当前x限制的数组。ylim也是如此。

Note that all of these commands apply to the current axis, so if you are creating subplots you will need to perform the scaling call once per axis as you build up your figure.

请注意,所有这些命令都适用于当前轴,因此,如果您正在创建子图,那么在您构建图形时,您将需要在每个轴上执行缩放调用。


Another useful fatures is the linkaxes command. This dynamically links the axis limits of two plots, including for programmatic resize commands like xlim and UI operations like pan and zoom. For example:

另一个有用的方法是linkaxes命令。这将动态地链接两个图的轴限值,包括xlim等编程调整大小命令和pan和zoom等UI操作。例如:

a(1) = subplot(211),plot(rand(10,1), rand(10,1)); %Store axis handles in "a" vector
a(2) = subplot(212),plot(rand(10,1), rand(10,1)): %

linkaxes(a, 'xy');

axis([0 30 0 8]);  %Note that all axes are now adjusted together
%Also try some manual zoom, pan operations using the UI buttons.

Looking at your code, post edit, your use of the plotmatrix function is complicating things. plotmatrix appears to create its own axes to work in, so you need to capture those handles and adjust them. (Also, in the future take h = zeros(..) out of the loop).

看看你的代码,发布编辑,你对plotmatrix函数的使用让事情变得复杂。plotmatrix似乎创建了自己的轴来工作,因此需要捕获这些句柄并对它们进行调整。(同样,在未来,h = zeros(..)退出循环)。

To get the handles to the plotmatrix created axes, use the second return argument, like this: [~, hAxes]=plotmatrix(current_rpm,current_torque);. Then collect those for future use.

要获取创建的plotmatrix的句柄,请使用第二个返回参数:[~,hAxes]=plotmatrix(current_rpm,current_torque);然后收集它们供将来使用。

Finally, the axis, xlim, ylim commands all act on the current axis, (see gca). However the plotmatrix axes are never current, so the axis command has not been affecting them. You can specify the axis to act on, like this: axis(hAxis, [0 30 0 8]);.

最后,轴、xlim、ylim命令都在当前轴上执行(参见gca)。但是plotmatrix坐标轴从来不是当前的,所以axis命令没有影响它们。可以指定要执行的轴,如下所示:axis(hAxis, [0 30 0 0 8]);

Putting this all together (an adding some variable definitions to get your code to execute), and this is what it looks like:

把所有这些放在一起(添加一些变量定义来执行代码),这就是它的样子:

%Define some dummy variables
current_rpm = rand(20,1)*30;
current_torque = rand(20,1)*8;
num_bins = 12;

%Loop to plot, collecting generated axis handles into "hAllAxes"
hAllAxes = [];
for i = 1:num_bins;
    subplot(4,3,i);
    [~, hCurrentAxes]=plotmatrix(current_rpm,current_torque);
    hAllAxes = [hAllAxes hCurrentAxes];  %#ok
end
linkaxes(hAllAxes,'xy');    
axis(hAllAxes,[0 30 0 8]);