16

I want to add a x-axis line at 0 to a Matlab figure so that I can compare my data to see if it is positive or negative when saving the figures to a jpg. What is the best way to do this? I know you can use line() but it just seems cumbersome because you need to specify the x and the y ranges. Is there an easier way?

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
O_O
  • 4,397
  • 18
  • 54
  • 69
  • See https://stackoverflow.com/q/17798093/7328782 for a similar question about vertical lines. – Cris Luengo Mar 26 '19 at 12:55
  • Possible duplicate of [How to draw horizontal and vertical lines in MATLAB?](https://stackoverflow.com/questions/28334660/how-to-draw-horizontal-and-vertical-lines-in-matlab) – SecretAgentMan Dec 17 '19 at 22:35

6 Answers6

32

There exist an undocumented function graph2d.constantline:

plot(-2:5, (-2:5).^2-1)

%# vertical line
hx = graph2d.constantline(0, 'LineStyle',':', 'Color',[.7 .7 .7]);
changedependvar(hx,'x');

%# horizontal line
hy = graph2d.constantline(0, 'Color',[.7 .7 .7]);
changedependvar(hy,'y');

screenshot

The nice thing is that it internally implements a listener for the axes limits (handles change like pan, zoom, etc..). So the lines would appear to extend to infinity.

Amro
  • 123,847
  • 25
  • 243
  • 454
  • Looks like my version of Matlab 7.0.1 R14 SP1 gives me a segmentation violation when I run your code and I don't get the x-axis line. :( – O_O Nov 15 '11 at 01:04
  • 2
    why the downvote, your version of [MATLAB](http://en.wikipedia.org/wiki/MATLAB#Release_history) is pretty old, and as it turns out doesn't run the above solution.. Plus as I mentioned, this is an undocumented feature – Amro Nov 15 '11 at 02:55
  • sorry, i didn't think it would do so. i tried to undo the upvote, but it caused the downvote instead. not very intuitive.. =/ now i can't do anything since the ans is locked until the post is edited and i don't have editing privileges. – O_O Nov 16 '11 at 01:24
  • Very nice solution! Exactly what i've been searching for a long time! – jrast Nov 27 '13 at 09:44
  • 14
    update: unfortunately the function was removed in R2014b (which introduced the new HG2 graphics system). – Amro Dec 11 '14 at 09:16
7

You could get this x range directly after the figure has been created. It goes a little something like this:

x=-2:5;
y=x.^2-1;

figure()
plot(x,y);

xlim = get(gca,'xlim');  %Get x range 
hold on
plot([xlim(1) xlim(2)],[0 0],'k')

enter image description here

Note that if you do any manual zooming out in the figure, the line might have to be redrawn to go over the entire new x range.

Vidar
  • 4,141
  • 5
  • 24
  • 30
6

A vline and hline command like in GNU R would be great, but I could not find a shorter solution than

plot(1:10,sin(1:10));
line(xlim,[0 0],'Color','r') 
Jonas Stein
  • 6,826
  • 7
  • 40
  • 72
6

I don't believe there is a built-in way that is more convenient. I use hline() and vline() from FileExchange, which work like a charm:

http://www.mathworks.com/matlabcentral/fileexchange/1039

John Colby
  • 22,169
  • 4
  • 57
  • 69
1
  1. Draw your data by plot() command or stem(). A figure window will open.
  2. Then on the figure window, click on the [insert] command from the
    menu bar, a drop-down menu will appear.
  3. From this menu click on the [line] command, now the shape of the
    cursor will change into a plus sign.
  4. Now you can draw a line anywhere you want, either horizontal or
    vertical or slanted.
  5. You can change the properties of line by right clicking on the
    line, a menu will appear from which you can choose your desires
    properties.
  6. If you want to have some ticks on the line then you can use add text option, and place text where ever you want.
  7. If you would like to have a code for your figure, click on [file] menu and then click on [generatecode] option, a new text editor
    window will open, you can save this code for further use. Good luck.
Jakob Runge
  • 2,287
  • 7
  • 36
  • 47
Mota Mota
  • 53
  • 1
  • 5
1

Since MATLAB R2018b there is yline for this purpose:

 yline(0)

draws a horizontal line at y==0.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120