Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a x-axis line to a figure? (matlab)

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?

like image 380
O_O Avatar asked Nov 10 '11 21:11

O_O


People also ask

How do I add a line to a figure in MATLAB?

yline( y ) creates a horizontal line at one or more y-coordinates in the current axes. For example, yline(2) creates a line at y=2 . yline( y , LineSpec ) specifies the line style, the line color, or both. For example, yline([12 20 33],'--b') creates three dashed blue lines.

How do you draw an X line in MATLAB?

xline( x ) creates a vertical line at one or more x-coordinates in the current axes. For example, xline(2) creates a line at x=2 . xline( x , LineSpec ) specifies the line style, the line color, or both. For example, xline([12 20 33],'--b') creates three dashed blue lines.

How do you mark X axis in MATLAB?

Label x-Axis of Specific PlotCall the tiledlayout function to create a 2-by-1 tiled chart layout. Call the nexttile function to create the axes objects ax1 and ax2 . Plot data into each axes, and create an x-axis label for the top plot.


1 Answers

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.

like image 132
Amro Avatar answered Sep 20 '22 06:09

Amro