Patch function doesnt shade the second plot.

2 次查看(过去 30 天)
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by