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?
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.
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.
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.
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');
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With