Line drawing of the upper axis (boxes)

I have a graph with two lines and two different along the X axis (different data units), which I draw as follows.

My problem is that I would also like to draw the top line of the box in black (horizontal), and not leave it β€œopen” as it is. It would be great if the line also had ticks along the x axis, as well as the lower horizontal axis.

Obviously, grid on does not work, because it draws ticks of the y1 axis on the right, and the y2 axis on the left, which I do not need.

Also, I think this worked in Matlab 2014: set(ax(2),'XAxisLocation','top','XTickLabel',[]); but in Matlab 2015a this is no longer there.

Here is an example:

 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 

enter image description here

+6
source share
2 answers

Based on this answer , you can simply add another axes to your plot and indicate 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:

In accordance with the explanation of the OP in the comment, you can draw the black axes β€œunder” the blue / orange, reordering the children of the figure, namely, after my code above, 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. 

enter image description here


By the way, I remember that I used a similar trick when I wanted to have small and large grids with different colors - each set of grid lines belonged to its own axes with its own color .

+3
source

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

 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 ; 
0
source

Source: https://habr.com/ru/post/987657/


All Articles