Patch function doesnt shade the second plot.

12 ビュー (過去 30 日間)
Tejas Mahadevan
Tejas Mahadevan 2023 年 2 月 7 日
コメント済み: Star Strider 2023 年 2 月 8 日
Hello, I am trying to plot 2 datasets on the same plot. I have 2 data sets that contain of means and standard deviations. I want to plot them so that the mean is plotted as a line plot and the standard deviation is plotted as a shaded area around it. I am using the patch function and i am able to get this done for one data set but when i try to do it for the second data set, the patch function doesnt shade in the area between the standard deviation, rather it just plots the standard deviation as 2 lines.
This is the code I am using.
plot(time,meanND);
Unrecognized function or variable 'time'.
hold on;
patch([time fliplr(time)], [meanND-stdev fliplr(meanND+stdev)], 'r', 'FaceAlpha',0.2, 'EdgeColor','none');
hold on;
plot(time, meanNDtest);
hold on;
patch([time fliplr(time)], [meanNDtest-stdevtest fliplr(meanNDtest+stdevtest)], 'r', 'FaceAlpha',0.2, 'EdgeColor','r');
hold off
I dont understand what I am doing wrong. Any help will be appreciated. I tried changing the patch arguments for the second plot. Nothing changes the shaded region. I am only able to change the opacity of the standard deviation lines.

採用された回答

Star Strider
Star Strider 2023 年 2 月 8 日
One of those vectors in the second plot likely has at least one NaN value.
Try something like this —
meanNDtest = fillmissing(meanNDtest, 'nearest');
stdevtest = fillmissing(stdevtest, 'nearest');
I have no idea where it is in your data, however I can reproduce the error you are getting by putting it at the end of the ‘ym’ vector here —
x = 1:10;
ym = rand(1,10);
ysd = 0.1+rand(1,10)/10;
figure
plot(x, ym, '-')
hold on
patch([x flip(x)], [ym+ysd flip(ym-ysd)],'r', 'FaceAlpha',0.5, 'EdgeColor','none')
hold off
title('Without NaN')
ym(10) = NaN; % NaN In 'ym'
figure
plot(x, ym, '-')
hold on
patch([x flip(x)], [ym+ysd flip(ym-ysd)],'r', 'FaceAlpha',0.5, 'EdgeColor','r')
hold off
title('With NaN')
If there is a NaN value in any of the patch arguments, the patch will not plot.
See the documentation on fillmissing for details.
.
  2 件のコメント
Tejas Mahadevan
Tejas Mahadevan 2023 年 2 月 8 日
that did it. there was a NaN value in the dataset. I did not know NaN values would cause this issue. Thank you so much for your help. I really appreciate it.
Star Strider
Star Strider 2023 年 2 月 8 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePolygons についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by