How do I shade the area between two curves on a plot that is generated by arrays?

For example:
x1 = [ 1 2 3 ];
y1 = [2 5 8];
x2 = [1 2.5 3.5];
y2 = [1 6 3];
plot(x1, y1)
hold on
plot(x2, y2)
Thanks for the help!

 採用された回答

Davide Masiello
Davide Masiello 2023 年 8 月 2 日
編集済み: Davide Masiello 2023 年 8 月 2 日
Use the patch function.
x1 = [1 2 3];
y1 = [2 5 8];
x2 = [1 2.5 3.5];
y2 = [1 6 3];
hold on
patch([x1 flip(x2)], [y1 flip(y2)],'y','EdgeColor','none')
plot(x1, y1,'b')
plot(x2, y2,'r')
box on
axis padded

4 件のコメント

Rowan Quintero
Rowan Quintero 2023 年 8 月 2 日
Awesome thanks for the response. However, what if the arrays are different lengths? I have one aray of 1250 and another of 2500
Still works
x1 = [1 2 3 4 5 6 7 8];
y1 = [2 5 8 11 14 17 20 23];
x2 = [1 2.5 3.5];
y2 = [1 6 3];
hold on
patch([x1 flip(x2)], [y1 flip(y2)],'y','EdgeColor','none')
plot(x1, y1,'b')
plot(x2, y2,'r')
box on
axis padded
Rowan Quintero
Rowan Quintero 2023 年 8 月 2 日
I am getting the error:
"error in horzcat:
dimensions in array being concatenated are not consistent"
Any ideas? I appreciate your responsiveness.
Rowan Quintero
Rowan Quintero 2023 年 8 月 2 日
Fixed: The arrays were 1250x1 instead of 1x1250. thanks!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by