使用一个y轴和两个x轴绘制两组数据的图表

3

我阅读了几篇关于设置两个数据的x轴的stackoverflow答案,以及mathworks.com上的一些教程,但我没有找到完全满足以下要求的方法:

正常绘制第一个数据集。 在图形的顶部创建第二个x轴,但使用现有的y轴来表示下一个数据集。 绘制第二个数据集,使其控制第二个x轴(比例等),并且不覆盖或重新调整现有的单个y轴。

这样做的原因是我想基于同一源数据绘制两组不同的类似直方图的值,因此频率分布在数量上相似,但bin大小/边缘的值不同。

我的备选方案是对第二个数据集的x数据进行点斜率缩放,但然后我仍需要创建第二个类似于如何在Matlab中插入两个X轴的x轴。

2个回答

7
你可以在第一个轴的上方创建第二个轴,位置相同,将该轴的XAxisLocation设置为'top',没有Color使其透明,没有yticks,并将其YLim与第一个轴相连。此外,我们可以链接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'})

enter image description here

Carl W(原帖作者)的编辑

当刻度间距上下不同的时候,以上代码会导致丑陋的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'});

这会提供 enter image description here

看起来不错。我几天内没有带有MATLAB的电脑,但在快速测试后可能会接受此答案。linkprop是我需要的魔法酱汁。 - Carl Witthoft
如果您想使用 plot,则可以在绘制对象创建后修改轴。 - Suever
我尝试过,但运气不太好。也许当时我在处理错误的轴属性集。无论如何,现在所有都运行得很顺畅。 - Carl Witthoft

1
这是我使用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');

我不觉得需要在这里添加图例,因为坐标轴和绘图颜色已经指示了图例。请参见下图:

twoXaxis1

但如果你仍然想要添加图例,可以使用以下方法:

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

输出:- twoXaxis2

1
啊,所以你旋转了整个东西!:-)。这是将plotyy变形为plotxx的一种方法。 - Carl Witthoft
新版的Matlab建议使用yyaxis,但是在使用后我无法旋转图表。有什么解决办法吗? - Gideon Kogan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接