Title: Matlab line plot overlaps axis when values are zero
Tags: matlab;plot;line;matlab-figure
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.
Comment: Duplicate of http://stackoverflow.com/q/3655256/2647279

Here is the accepted answer: 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.
Comment for this answer: I have marked this as the answer as it is the official way to do it, although both work. Thanks :D
Comment for this answer: +1 I didn't know about that property. Seems like the way to go. Just a nitpick: this doesn't allow you to control the x- and y-axis behaviour independently

Here is another answer: 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)
Comment for this answer: You missed the fact that the line also overlaps the y-axis by a few pixels in the top-left corner ;). Seems a bit brute-force way to solve the problem ...
Comment for this answer: @BasSwinckels Thanks. I've added the y axis part, although the OP only asked for the x axis. And yes, it's a workaround rather than a solution.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:02/28/14', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<matlab><plot><line><matlab-figure>', 'Title': 'Matlab line plot overlaps axis when values are zero', 'ViewCount': 966, 'id': 22093954, 'user_AboutMe': '<p>PhD student - climatology</p>\n', 'user_AccountId': 3888907.0, 'user_DisplayName': 'emmalgale', 'user_Reputation': 289.0, 'user_Views': 25.0, 'user_id': 3283542.0}}