Vertical grid line for x=0

121 visualizzazioni (ultimi 30 giorni)
Jan w
Jan w il 1 Gen 2017
Commentato: Star Strider il 17 Dic 2020
I am using horizontal gridlines for a plot.
ax = gca;
ax.XGrid = 'off';
ax.YGrid = 'on';
How can I add only one vertical line through x=0?

Risposta accettata

Star Strider
Star Strider il 1 Gen 2017
You can plot a vertical line anywhere by duplicating the x-coordinate and plotting it against ylim:
figure(1)
plot([0 0], ylim, '-r')
ax = gca;
ax.XGrid = 'off';
ax.YGrid = 'on';

Più risposte (2)

Image Analyst
Image Analyst il 1 Gen 2017
The best answer is to use YAxisLocation:
theta = linspace(-pi, pi, 800);
plot(theta, sin(theta), 'b-') % Plot something.
% Make axes go through origin instead of left and bottom sides of axes box.
ax = gca;
ax.XAxisLocation = 'origin'
ax.YAxisLocation = 'origin'
  4 Commenti
Jan w
Jan w il 2 Gen 2017
Yes this works fine for me!
Image Analyst
Image Analyst il 2 Gen 2017
You're welcome. Maybe you could "Vote" for the answer (under my logo).

Accedi per commentare.


the cyclist
the cyclist il 1 Gen 2017
Modificato: the cyclist il 1 Gen 2017
line([0 0],[0 1],'Color','Black')
See line for details of setting the properties of the line.
  2 Commenti
Brian Russell
Brian Russell il 16 Dic 2020
You simply need to write yline(0) or xline(0) for vertical and horizontal lines.
Star Strider
Star Strider il 17 Dic 2020
Note that xline and yline were introduced in R2018b, years after this was posted.

Accedi per commentare.

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by