Plot dashed lines to given y value on a graph

How can I get dashed lines from each axis out to a point at a graph, given by a specified y value?
Somehow i would also like to get the corresponding x value represented.
Thanks for any help. For use in an example:
x = [0:1:25];
y = x * pi ;
plot(x,y)

 採用された回答

Walter Roberson
Walter Roberson 2021 年 4 月 20 日

0 投票

x = [0:1:25];
y = x * pi ;
stem(x,y, '--')

4 件のコメント

Walter Roberson
Walter Roberson 2021 年 4 月 20 日
Somehow i would also like to get the corresponding x value represented.
annotation() maybe? text() maybe?
Rudolf
Rudolf 2021 年 4 月 20 日
編集済み: Rudolf 2021 年 4 月 20 日
Stem is only plotting the graph sort of. I was looking for a way to get one dashed line from y-axis, for example at value 30, to go out to where it hits the graph, then a dashed line from this point down to the x-axis. And then in some way represent that x-value. I will check out text(), thanks.
Walter Roberson
Walter Roberson 2021 年 4 月 20 日
x = [0:1:25];
y = x * pi ;
plot(x, y);
hold on
ytarg = 30;
[~,yidx] = min(abs(y-ytarg));
xtarg = x(yidx);
plot([0, xtarg xtarg], [30 30 0], '--');
text(2, 50, sprintf('x=%g', xtarg), 'HorizontalAlignment', 'center')
plot([2 xtarg], [50 y(yidx)], '-k');
hold off
With this code, you would need to use a finer-grain x to have the points match up better.
You could interpolate to find a more accurate x, but the ease of doing that would depend upon whether the y is monotonic. y is monotonic in this case, so it could be done easily with xi = interp1(y,x,30)
Rudolf
Rudolf 2021 年 4 月 20 日
Perfect!! Thank you so much! This solved the challenge.

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

2021 年 4 月 20 日

コメント済み:

2021 年 4 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by