How to add a dashed horizontal line at 0 using plot function?

 採用された回答

Adam Danz
Adam Danz 2019 年 9 月 29 日
編集済み: Adam Danz 2019 年 10 月 2 日

3 投票

Here are 3 options to add a dashed horizontal line to a plot. The 1st option is recommended if you're using matlab r2018b or later.
% Use yline() to plot a horizontal line at y=0 (requires r2018b or later)
% https://www.mathworks.com/help/matlab/ref/yline.html
% This will automatically adjust if your xlim changes.
yline(0,'k--')
% Plot a line at y=0 that spans from x=0 to x=5
plot([0,5],[0,0], 'k--')
% Plot a line at y=0 that spans the current x-limits of your axes
plot([min(xlim()),max(xlim())],[0,0], 'k--')

3 件のコメント

shahriar sowad
shahriar sowad 2021 年 6 月 30 日
@Adam Danz i wanted to plot a line at y=0.2
so i wrote plot([0,8000],[0.2 ,0.2])
but it is giving me a line at y=0;what should i change in the code?
Adam Danz
Adam Danz 2021 年 7 月 1 日
If you zoom into the line you will see that it is plotted at y=0.2.
shahriar sowad
shahriar sowad 2021 年 7 月 1 日
yes,got it,thanks

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDiscrete Data Plots についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by