How to shade several areas in a plot?

169 ビュー (過去 30 日間)
Malahat Mehraban
Malahat Mehraban 2021 年 12 月 13 日
コメント済み: Malahat Mehraban 2021 年 12 月 13 日
Hello everyone,
I want to shade some points in a figure. These points are for example 35 in a plot with the length of 600 point. How can I plot these shades in a for loop?
I actually expect to get somthing like the picture that I have attached. I have a figure with three subplots and I want to shade 35 points of these plots.
Thank you in advance for your helps.

採用された回答

Star Strider
Star Strider 2021 年 12 月 13 日
Try this —
x = 210:0.5:227;
y = randn(size(x));
bands = [214 216; 221 223]; % X-Coordinates Of Band Limits: [x(1,1) x(1,2); x(2,1) x(2,2)]
figure
plot(x, y)
hold on
xp = [bands fliplr(bands)]; % X-Coordinate Band Definitions
yp = ([[1;1]*min(ylim); [1;1]*max(ylim)]*ones(1,size(bands,1))).'; % Y-Coordinate Band Definitions
for k = 1:size(bands,1) % Plot Bands
patch(xp(k,:), yp(k,:), [1 1 1]*0.25, 'FaceAlpha',0.5, 'EdgeColor',[1 1 1]*0.25)
end
hold off
This is fully adaptive to the size of the plot limits, requiring only that the ‘bands’ matrix be defined.
I doubt if there is a way to do this without the loop.
.
  2 件のコメント
Malahat Mehraban
Malahat Mehraban 2021 年 12 月 13 日
It was a great solution, Thank you.
Star Strider
Star Strider 2021 年 12 月 13 日
As always, my pleasure!
.

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

その他の回答 (2 件)

Chunru
Chunru 2021 年 12 月 13 日
x = 210:.5:218;
y =randn(size(x));
plot(x, y); hold on
yl = ylim;
% Use the patch for the box
patch([212 213 213 212], [yl(1) yl(1) yl(2) yl(2)], [0.3 0.3 0.3], 'FaceAlpha', 0.3 )
  1 件のコメント
Malahat Mehraban
Malahat Mehraban 2021 年 12 月 13 日
Thank you :)

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


Voss
Voss 2021 年 12 月 13 日
Look into patch or fill, which are different ways to do basically the same thing.
In addition to the x- and y-coordinates of the rectangles you want to add to the plots, you'll need to specify the 'AlphaData' property for the transparency and the 'EdgeColor' property for the red lines around the edges. A list of patch properties is here.
  1 件のコメント
Malahat Mehraban
Malahat Mehraban 2021 年 12 月 13 日
Thanks Benjamin :) the like was informative.

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

カテゴリ

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