How to specify an x-range for a yline?

we have the following code;
x = -2:0.25:2;
[X,Y] = meshgrid(x);
Z = X.*exp(-X.^2-Y.^2);
contour(X,Y,Z,30)
yl = yline(0,'--','y = 0','LineWidth',3);
It returns the following graph:
How to specify a range for the line? for example if i want to see the line only between x=-1 to x=1?

 Réponse acceptée

Walter Roberson
Walter Roberson le 22 Mar 2021

0 votes

You cannot do that with yline() . You need to line() or plot() the line into place, and text() the label into place.

3 commentaires

Dear Walter,
Thank you for your answer!
I tried with the following code but instead of a horizontal line if gave a vertical line! How to fix it, any suggestions?
x = -2:0.25:2;
[X,Y] = meshgrid(x);
Z = X.*exp(-X.^2-Y.^2);
contour(X,Y,Z,30)
y = 1;
x1 = -1;
x2 = 1;
hold(gca,'on')
plot([y; y], [x1; x2], 'LineWidth',3)
cls
Best regards
/Hedi
plot([x1; x2], [y; y], 'LineWidth',3)
Harr
Harr le 22 Mar 2021
Thankl you very much ^_^

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by