Search code examples
matlabmatlab-figure

xaxis/yaxis lines in matlab plot


couldn't find this:

I would like to plot a line on X and Y axis that always fits to 100% to the width or height of the figure.

figure; hold on;
plot(rand(1,100));
line(xlim,[.5 .5],'Color','red');
line([50 50],ylim,'Color','red');
pause(.5)
xlim([1 200]);% lines should now automatically extend

with grid on it's possible to get a grid that scales automatically, however it seems impossible to only limit the grid to the X/Y axes. Ideas?

bsdf

after scaling:

asdf

what I would prefer:

enter image description here


Solution

  • The functions xline and yline were introduced in MATLAB R2018b, and do exactly as you need.

    Furthermore, it is possible to add a (text) label to the line.