How to draw lines in matlab figure

379 visualizzazioni (ultimi 30 giorni)
Safi ullah
Safi ullah il 15 Apr 2017
Risposto: Steven Lord il 11 Nov 2019
Hi everyone, I plot simple figure in matlab by using plot(x,y).now I need to draw verticale dashed lines like grid lines at some specific points on x-axis.,what should I do? thanks

Risposta accettata

Star Strider
Star Strider il 15 Apr 2017
Try this:
x = 0:20;
y = sin(x*pi/9);
plot(x, y);
hold on
plot([1 1]*4.5, ylim, '--k') % First Vertical Line at ‘x=4.5’
plot([1 1]*13.5, ylim, '--k') % First Vertical Line at ‘x=13.5’
hold off
Also, I like your Emperor penguins!
  2 Commenti
Star Strider
Star Strider il 11 Nov 2019
plot(xlim, [1 1]*4.5, '--k') % First Horizontal Line at ‘y=4.5’
plot(xlim, [1 1]*13.5, '--k') % First Horizontal Line at ‘y=13.5’

Accedi per commentare.

Più risposte (1)

Steven Lord
Steven Lord il 11 Nov 2019
You could either turn the grid on or use the xline and/or yline functions.
grid on
xline(4.5, 'r:')
yline(2*pi, 'c--', 'LineWidth', 2)
With xline and yline, if you pan or zoom the axes the lines still extend to the limits of the axes.


Scopri di più su Visual Exploration in Help Center e File Exchange


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by