I have line and want to colour the area behind the line (beyond the x axis) how can I do this

7 ビュー (過去 30 日間)
Katherine
Katherine 2023 年 11 月 23 日
コメント済み: Adam Danz 2023 年 11 月 24 日
I have a line in the format y= mx+c, and I want to shade/colour the area 'behind' the line depending on the direction of my line. I can only see functions that shade/colour the axis from line to x axis. Is there a way of doing what I am describing
  6 件のコメント
Image Analyst
Image Analyst 2023 年 11 月 23 日
Not really? Did you expand the comments to see all of them or only see the last one?
Adam Danz
Adam Danz 2023 年 11 月 24 日
Thanks @Katherine, that makes sense to me. Looks like @Star Strider's answer addresses your question.

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

回答 (1 件)

Star Strider
Star Strider 2023 年 11 月 23 日
編集済み: Star Strider 2023 年 11 月 23 日
I am not completely certain what ‘behind’ the line means, however from your comments with respect to the x-axis, I guess that you mean fro the y-axis to the line.
If so, try this —
x = linspace(0, 10).';
m = randn
m = -0.8135
c = randn
c = 0.4800
y = m*x + c;
figure
plot(x, y)
hold on
patch([x; zeros(size(x))], [y; flip(y)], 'r', 'FaceAlpha',0.5, 'EdgeColor','none')
hold off
m = randn
m = 1.6675
c = randn
c = -0.4378
y = m*x + c;
figure
plot(x, y)
hold on
patch([x; zeros(size(x))], [y; flip(y)], 'r', 'FaceAlpha',0.5, 'EdgeColor','none')
hold off
Please clarify if you intend something else.
EDIT — (23 Nov 2023 at 15:27)
Added second plot with different parameters.
EDIT — (23 Nov 2023 at 17:25)
Added ‘Entire Underneath the Line’ and ‘Entire Above the Line’ in the following code sections —
x = linspace(0, 10).';
m = randn
m = -0.0184
c = randn
c = 0.8392
y = m*x + c;
figure
plot(x, y)
hold on
patch([x; flip(x)], [y; zeros(size(y))+min(ylim)], 'g', 'FaceAlpha',0.5, 'EdgeColor','none')
hold off
title(sprintf('Entire Underneath the Line\ny = %.3f*x %+.3f', m,c))
m = randn
m = 1.4742
c = randn
c = -1.7464
y = m*x + c;
figure
plot(x, y)
hold on
patch([x; flip(x)], [y; zeros(size(y))+max(ylim)], 'g', 'FaceAlpha',0.5, 'EdgeColor','none')
hold off
title(sprintf('Entire Above the Line\ny = %.3f*x %+.3f', m,c))
.

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by