Drawing the top axis (box) line

会有一股神秘感。 提交于 2019-12-01 17:04:55

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',[]);

Edit:

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.


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.

If you want to avoid adding another set of axes, you can still use ax(2) but you need to make it visible first:

ax(1).Box = 'off';
ax(2).Box = 'off';
ax(2).XAxis.Visible = 'on';
ax(2).XAxisLocation = 'top';
ax(2).XTickLabel = [];
ax(2).XTick = ax(1).XTick ;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!