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?

 採用された回答

Star Strider
Star Strider 2017 年 1 月 1 日

0 投票

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';

その他の回答 (2 件)

Image Analyst
Image Analyst 2017 年 1 月 1 日

4 投票

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 件のコメント

Jan w
Jan w 2017 年 1 月 2 日
編集済み: Jan w 2017 年 1 月 2 日
Thanks! How can I put only an axis thorugh x=0 without having to move the whole YAxis? It should stay on the left side. I can't find any axis propertys: https://de.mathworks.com/help/matlab/ref/axes-properties.html Regards!
Image Analyst
Image Analyst 2017 年 1 月 2 日
I don't understand. Normally the Y axis is at the left hand side of the axes box. If you want it to remain there, then just don't set YAxisLocation. If you want to move it so that it crosses at the x=0 line, then set YAxisLocation = 'origin'.
The only way to not move the Y axis and have it still be on the left hand side, and to have that left hand side also be the x=0 location is to set xlim to be 0:
% Get current axes limits
xl = xlim();
% Set xl(1) = 0
xl(1) = 0;
% Update the x axes limits.
xlim(xl);
Is that what you want?
Jan w
Jan w 2017 年 1 月 2 日
Yes this works fine for me!
Image Analyst
Image Analyst 2017 年 1 月 2 日
You're welcome. Maybe you could "Vote" for the answer (under my logo).

サインインしてコメントする。

the cyclist
the cyclist 2017 年 1 月 1 日
編集済み: the cyclist 2017 年 1 月 1 日

0 投票

line([0 0],[0 1],'Color','Black')
See line for details of setting the properties of the line.

2 件のコメント

Brian Russell
Brian Russell 2020 年 12 月 16 日
You simply need to write yline(0) or xline(0) for vertical and horizontal lines.
Star Strider
Star Strider 2020 年 12 月 17 日
Note that xline and yline were introduced in R2018b, years after this was posted.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGraphics Object Programming についてさらに検索

タグ

質問済み:

2017 年 1 月 1 日

コメント済み:

2020 年 12 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by