Patch function doesnt shade the second plot.
12 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Tejas Mahadevan
il 7 Feb 2023
Commentato: Star Strider
il 8 Feb 2023
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);
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.
0 Commenti
Risposta accettata
Star Strider
il 8 Feb 2023
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.
.
2 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Polygons in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!