设置轴的最小值和最大值以保持不变 [英] setting the axis min and max values to stick

查看:103
本文介绍了设置轴的最小值和最大值以保持不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个(3,4)子图,每个子图都显示了散点图.散点图的范围各不相同,因此我的某些绘图具有x(0-30)和y(0-8)轴,但有些绘图具有x(18-22)和y(4-7).我将xlim设置为[0 30],将ylim设置为[0 8],但这将我的轴设置为从不低于0,高于30等.

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.

如何将每个图的原点的轴设置为粘"在(0,0),Y的轴设置为粘",X的轴设置为粘"为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.

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]);

推荐答案

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

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

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

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


通常只使用以下代码设置两个x限制之一:


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

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

这利用了xlim的零输入参数调用约定的优势,该约定返回当前x个限制的数组. ylim也是如此.

This takes advantage of xlims zero input argument calling convention, which returns an array of the current x limits. The same works with 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.

另一个有用的功能是linkaxes命令.这将动态链接两个图的轴限制,包括用于程序调整大小命令(如xlim)和UI操作(如平移和缩放)的轴范围.例如:

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:

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.


查看您的代码,进行后期编辑,使用plotmatrix函数会使事情复杂化. plotmatrix似乎创建了自己的轴以供使用,因此您需要捕获这些手柄并进行调整. (此外,将来将h = zeros(..)移出循环).


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轴的句柄,请使用第二个return参数,如下所示:[~, hAxes]=plotmatrix(current_rpm,current_torque);.然后收集它们以备将来使用.

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.

最后,axisxlimylim命令全部作用于当前轴(请参见gca).但是plotmatrix轴从不最新,因此axis指令并未对其产生影响.您可以指定要作用的轴,如下所示:axis(hAxis, [0 30 0 8]);.

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]);.

将所有内容放在一起(添加一些变量定义以使您的代码得以执行),这就是它的样子:

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]);

这篇关于设置轴的最小值和最大值以保持不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆