Question

I have plotted a figure with multiple lines on it, and I have noticed that the lines for the plot overlap the x-axis when they are zero. Is there a way that I can essentially get the x-axis to plot on the top, rather than the lines?

Here is a MWE that does the same thing (I haven't put my exact code up as my dataset is quite big).

xdata=1:1:10;
ydata=[1;0.8;0.6;0.4;0.2;0;0;0;0;0];
line(xdata,ydata)

After I plot the lines (multiple per plot in my case), I do various other things with the axes so I get what I need (including adding a secondary set of axes). None of this seems to make any difference as to whether the x-axis is plotted on top of the lines or not.

I did have a search online but couldn't find anything to do with this.

Was it helpful?

Solution

The answer given by Luis is a nice workaround, but the official way to solve this problem is to use the layer property of the axis object, see the manual. To plot the axis on top of the data you do

set(gca,'Layer','top')

To automatically do this for all your plots, you can put the following line in your startup.m:

set(0,'DefaultAxesLayer','top')

This kind of answers you do not make up yourself, I only discovered this trick after asking more or less the same question on comp.soft-sys.matlab many years ago. See also this SO question.

OTHER TIPS

After having plotted all your lines, plot a line on the x axis with the same color as the axis:

hold on
a = axis; %// gives xmin xmax ymin ymax
cx = get(gca,'Xcolor'); %// color of x axis
plot([a(1) a(2)], [a(3) a(3)], 'color', cx)

If the lines also overlap with the y axis and you also want that axis to appear on top, add the following:

cy = get(gca,'Ycolor'); %// color of y axis
plot([a(1) a(1)], [a(3) a(4)], 'color', cy)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top