Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab line plot overlaps axis when values are zero

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.

like image 497
emmalgale Avatar asked Feb 13 '23 15:02

emmalgale


1 Answers

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.

like image 150
Bas Swinckels Avatar answered Feb 16 '23 08:02

Bas Swinckels