【问题标题】:setting the axis min and max values to stick将轴最小值和最大值设置为坚持
【发布时间】:2013-03-11 04:52:22
【问题描述】:

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

如何将每个绘图的原点设置为“固定”在 (0,0),将 Y 设置为 8,X 设置为 30。

TIA 寻求帮助


根据回答的评论更新:
下面的代码仍然有同样的问题

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

【问题讨论】:

    标签: matlab axes subplot


    【解决方案1】:

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

    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 限制之一,我通常使用这样的代码:

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

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

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


    另一个有用的功能是linkaxes 命令。这会动态链接两个绘图的轴限制,包括用于编程调整大小命令(如xlim)和 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.
    

    查看您的代码,编辑后,您对plotmatrix 函数的使用使事情变得复杂。 plotmatrix 似乎创建了自己的轴来工作,因此您需要捕获这些句柄并调整它们。 (另外,将来将h = zeros(..) 排除在外)。

    要获取plotmatrix 创建的轴的句柄,请使用第二个返回参数,如下所示:[~, hAxes]=plotmatrix(current_rpm,current_torque);。然后收集这些以备将来使用。

    最后,axisxlimylim 命令都作用于当前轴,(参见gca)。然而plotmatrix 轴永远不会是最新的,所以axis 命令并没有影响它们。您可以指定要作用的轴,如下所示:axis(hAxis, [0 30 0 8]);

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

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

    【讨论】:

    • 感谢您的回复。我已经尝试过了,但这只是将最小 x 值设置为 0,但我仍然得到 x 轴在 18-22 范围内的图。我需要我的所有子图的比例完全相同。有什么想法吗?
    • 确保在绘制数据后设置轴。
    • 添加了一些更新。我怀疑linkaxes 真的是你想要的。
    • 为了增加在 for 循环中调用子图的混乱,所以链接轴在这里不起作用: for i = 1:num_bins; h = 零(ceil(num_bins),1); h(i)=子图(4,3,i); plotmatrix(current_rpm,current_torque) xlim([0 max(xlim)]); ylim([0 最大值(ylim)]);结束
    • 为什么不能把linkaxes(h,'xy'); 紧跟在循环后面?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    相关资源
    最近更新 更多