Patch function doesnt shade the second plot.

12 visualizaciones (últimos 30 días)
Tejas Mahadevan
Tejas Mahadevan el 7 de Feb. de 2023
Comentada: Star Strider el 8 de Feb. de 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);
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.

Respuesta aceptada

Star Strider
Star Strider el 8 de Feb. de 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.
See the documentation on fillmissing for details.
.
  2 comentarios
Tejas Mahadevan
Tejas Mahadevan el 8 de Feb. de 2023
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 el 8 de Feb. de 2023
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by