【问题标题】:Plot two sets of data with one y-axis and two x-axes用一个 y 轴和两个 x 轴绘制两组数据
【发布时间】:2016-10-27 12:34:06
【问题描述】:

我已经阅读了几个关于为数据设置两个 x 轴的 SO 答案,以及 mathworks.com 上的一些教程,但我没有看到完全执行以下操作的方法:

正常绘制第一个数据集。 在图表的顶部创建第二个 x 轴,但将现有的 y 轴用于下一个数据集。 绘制第二个数据集,使其控制第二个 x 轴(缩放等),并且不会覆盖或重新缩放现有的单个 y 轴。

这样做的原因是我想基于相同的源数据集绘制两组不同的直方图值,因此频率分布的幅度相似,但 bin 大小/边缘的值不同.

我的后备方案是对第二个数据集的 x-data 进行点斜率缩放,但是我仍然需要创建类似于 How to insert two X axis in a Matlab a plot 的第二个 x 轴。

【问题讨论】:

    标签: matlab plot matlab-figure


    【解决方案1】:

    您可以在第一个轴(在同一位置)之上创建第二个轴,将 XAxisLocation 设置为 'top',没有 Color,因此它是透明的,没有 yticks,并且它是 @987654322 @ 链接到第一个轴的那个。此外,我们可以链接Position 值,以确保如果我们调整其中一个轴的大小,它们会一起调整大小以保持其外观。

    figure;
    
    % Create the first axes
    hax1 = axes();
    
    % Plot something here
    xdata = 1:10;
    hplot1 = line(xdata, log(xdata));
        
    % Create a transparent axes on top of the first one with it's xaxis on top
    % and no ytick marks (or labels)
    hax2 = axes('Position', get(hax1, 'Position'), ...  % Copy position
                'XAxisLocation', 'top', ...             % Put the x axis on top
                'YAxisLocation', 'right', ...           % Doesn't really matter
                'xlim', [2 20], ...                     % Set XLims to fit our data
                'Color', 'none', ...                    % Make it transparent
                'YTick', []);                           % Don't show markers on y axis
                
    % Plot data with a different x-range here
    
    hplot2 = line(xdata * 2, log(flip(xdata)), 'Color', 'r', 'Parent', hax2);
    
    % Link the y limits and position together
    linkprop([hax1, hax2], {'ylim', 'Position'});
    
    % Draw some labels
    xlabel(hax1, 'Blue Line')
    xlabel(hax2, 'Red Line')
    ylabel(hax1, 'Some Value')
    
    % Add a legend? Why not?!
    legend([hplot1, hplot2], {'Blue', 'Red'})
    

    由 Carl W(OP)编辑

    当刻度间距上下不一致时,上面的代码会导致丑陋的 XTicks。 我在 matlab remove only top and right ticks with leaving box on 找到了解决方法。我将上面的代码稍微修改为

    figure
    xdata = 1:10;
    plot(xdata)
    % get handle to current axes
    hax1 = gca;
    % set box property to off 
    set(hax1,'box','off','color','white')
    
    hax2 = axes('Position', get(hax1, 'Position'),'box','off', ...  % Copy position
                'XAxisLocation', 'top', ...             % Put the x axis on top
                'YAxisLocation', 'right', ...           % Doesn't really matter           
                'Color', 'none', ...                    % Make it transparent
                'YTick', []); 
    

    警告:这将plot 一起使用,这将覆盖现有的轴分配。

    由于没有 points 函数(愚蠢的 MathWorks),我不得不使用 line(x,y,'linestyle','none','marker','x','parent',hax2) 来获得积分。

    hplot2 = line(5:25, log((5:25)), 'Color', 'r', 'Parent', hax2);
    
    linkprop([hax1,hax2],{'ylim','Position'});
    

    这给了

    【讨论】:

    • 看起来不错。几天我不会在带有 MATLAB 的计算机上,但在快速运行后可能会接受这个答案。 linkprop 是我需要的魔法酱。
    • @CarlWitthoft 如果您想使用plot,您可以在创建绘图对象之后修改轴。
    • 我试过了,没有运气。也许我当时正在处理错误的轴属性设置。无论如何,我已经让这一切顺利进行。
    【解决方案2】:

    这是我使用plotyy 的老生常谈的方法。

    代码:-

    %Random values for axes
    BotttomXaxis = 1:10;
    Yaxis =1:3:30;
    TopXaxis = (11:20:200).^3;
    
    [ax,H1,H2]= plotyy(Yaxis,BotttomXaxis,Yaxis,TopXaxis)
    view([90 -90])
    
    % Now labeling the axes, notice carefully where I have written to write y and x labels.
    xlabel('Enter ylabel of the y-axis here')
    ylabel(ax(1), 'Enter xlabel of the bottom x-axis here');
    ylabel(ax(2), 'Enter xlabel of the top x-axis here');
    

    我觉得没有必要在此处添加图例,因为轴和绘图颜色已经指示了图例。见下图:

    但如果你还想添加图例,可以使用如下:

    legend(H1,'Legend of Bottom X-axis','Location','northeast');
    legend(H2,'Legend of Top X-axis','Location','northeast');
    %Specifying the Legend location is necessary here
    

    输出:-

    【讨论】:

    • 啊,所以你旋转了整个东西! :-) 。这是将plotyy 变形为plotxx 的一种方式。
    • 新的 matlab 建议使用 yyaxis 但使用后我无法旋转绘图。有什么解决办法吗?