绘制上轴(框)线 [英] Drawing the top axis (box) line

查看:91
本文介绍了绘制上轴(框)线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两条线和两个不同的x轴(不同的数据单位)的图,如下图所示.

I have a plot with two lines and two different x-axis (different data units), which I plot like the following.

我的问题是,我也想(水平)将框的顶部绘制为黑色,而不是像现在一样将其打开".如果该行也具有x轴刻度线,并且与底部水平轴线相同,那就太好了.

My problem is that I would like to draw the top line of the box black as well (horizontally), and not leave it "open" like it is. It would be great if the line had the x-axis ticks as well, same as the bottom horizontal axis line.

很显然,grid on不起作用,因为它在右侧绘制了y1轴刻度,在左侧绘制了y2轴刻度.

Obviously, grid on doesn't work, because it draws the y1-axis ticks on the right and the y2-axis ticks on the left, which I don't want.

此外,我认为在Matlab 2014中,此方法有效:set(ax(2),'XAxisLocation','top','XTickLabel',[]);,但在Matlab 2015a中不再适用.

Also, I think in Matlab 2014, this worked: set(ax(2),'XAxisLocation','top','XTickLabel',[]); but it doesn't anymore in Matlab 2015a.

这里是例子:

figure(1);
x = [0, 1, 2, 3];
y_1 = [3, 2, 1.5, 1];
y_2 = [0, 0.5, 0.7, 0.9];
parula_blue = [0, 0.447, 0.741]; parula_red = [0.85, 0.325, 0.098];

[ax, h1, h2] = plotyy(x, y_1, x, y_2);
set(get(ax(1),'Ylabel'),'String','Data 1', 'Color', 'k');
set(h1,'LineWidth',2,'LineStyle','-','Color',parula_blue,'DisplayName', 'Name 1');
set(ax(1),'ycolor',parula_blue);
set(ax(1), 'YTick', [0 1 2 3 4]);
set(ax(1), 'ylim', [0 4]);

set(get(ax(2),'Ylabel'),'String','Data 2', 'Color', 'k');
set(h2,'LineWidth',2,'LineStyle','--','Color',parula_red,'DisplayName','Name 2');
set(ax(2),'ycolor',parula_red);
set(ax(2),'YDir','reverse');
set(ax(2), 'YTick', [0 0.2 0.4 0.6 0.8 1]);

xlabel('X axis desc')
legend('show')
set(ax, 'XTick', x)

set(ax(1),'Box','off') % Turn off box of axis 1, which removes its right-hand ticks
set(ax(2),'Box','off') % Turn off box of axis 2, which removes its left-hand   ticks

推荐答案

基于此答案,您只需将另一个axes添加到绘图中,并指定其水平轴位于顶部(此代码位于代码的末尾):

Based on this answer, you can simply add another axes to your plot, and specify that its horizontal axis is at the top (this code goes at the end of your code):

hBox = axes('xlim', [x(1) x(end)],'XTick', x, 'YTick',[],'XAxisLocation', 'top',...
            'XTickLabel',[]);

根据OP在注释中的说明,可以通过对图的子级进行重新排序来绘制蓝色\橙色下方"的黑轴,即,在我上面的代码之后,还添加:

As per the OP's clarification in the comment, it is possible to draw the black axes "underneath" the blue\orange by reordering the children of the figure, namely, after my above code, add also:

uistack(hBox,'bottom'); %// This sends the black axes to the back.
ax(1).Color = 'none';   %// This makes the plot area transparent for the top axes, so 
                        %// that ticks belonging to the black axes are visible.

顺便说一句,我记得当我想使用颜色不同的次要和主要网格线时,我也曾使用过类似的技巧-每组网格线都以自己的color属于自己的轴.

BTW, I remember using a similar trick when I wanted to have minor and major gridlines with different colors - each set of gridlines belonged to its own axes with their own color.

这篇关于绘制上轴(框)线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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