フィルターのクリア

How to shade the area between two lines (constructed by large number of data points)

29 ビュー (過去 30 日間)
I have seen some examples in which mostly MATLABer's have used curve1 and curve2 as an equation. and then using the flip method they shaded the required area between these two curves.
In my case I don't have any equation but a large number of data points. by using simple plot(xdata,ydata) I am ploting these lines. Now my question is how can I shade the areas between two or three lines drawn by plot command.

採用された回答

Star Strider
Star Strider 2023 年 5 月 27 日
It works the same way with data points as with functions —
x = linspace(0, 10, 50).'; % Assume Column Vectors
y1 = rand(size(x));
y2 = rand(size(x))+1;
y3 = rand(size(x))+2;
figure
plot(x, y1)
hold on
plot(x, y2)
patch([x; flip(x)], [y1; flip(y2)], 'r', 'EdgeColor','none', 'FaceAlpha',0.5)
patch([x; flip(x)], [y2; flip(y3)], 'g', 'EdgeColor','none', 'FaceAlpha',0.5)
hold off
grid
Most MATLAB data are column vectors, so I am using that convention here. For row vectors, replace the semicolons (;) with commas (,) in the patch calls.
Note that the independent variable vector (‘x’ here) must be monotonically increasing (or decreasing) for patch to work correctly. It cannot be random. If it is random, create a matrix and then sort it:
A = [x(:) y1(:) y2(:)];
As = sortrows(A,1);
xs = As(:,1);
y1s = As(:,2);
y2s = As(:,s);
Then use the sorted vectors as the patch arguments.
.
  2 件のコメント
Faisal
Faisal 2023 年 5 月 27 日
@Star Strider Thanks alot. It worked for me.
Star Strider
Star Strider 2023 年 5 月 27 日
As always, my pleasure!

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2023 年 5 月 27 日

カテゴリ

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

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by