How to fill between two lines?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Hi, I have been trying to fill between two lines using the fill function with facealpha. However, the plot does not show the shaded area between the two black lines. This is my code and graph currently. I am wanting the standard devation to be shaped and the mean line (red) to be plotted. Thanky for your help!
meanpt=nanmean(pt(:,3:17),2)
means=nanmean(s(:,3:17),2)
%standard deviation
sdpt=nanstd(pt(:,3:17)')
sds=nanstd(s(:,3:17)')
%plot lines
plot(means,meanpt,'r')
hold on
plot(s_std18,pt_std18,'g','Linestyle','--') %plus on estandard deviation
hold on
plot(s_std28,pt_std28,'b','Linestyle','--') %-1std
%add fill between standard deviation lines
inBetween=[pt_std18,fliplr(pt_std28)];
xinBetween=[s_std18,fliplr(s_std28)];
fill(xinBetween,inBetween,'b','Facealpha',0.2)

0 comentarios
Respuestas (1)
Star Strider
el 26 de Mzo. de 2021
It appears from the nanmean and nanstd calls that the results of ‘meanpt’, ‘means’, and the others are all column vectors, so fliplr would have no effect.
Instead, use:
inBetween=[pt_std18; flipud(pt_std28)];
xinBetween=[s_std18; flipud(s_std28)];
That aside, fill may still have problems with the black lines, since there actually appears to be only one of them. Consider using sort or sortrows on your data first, sorting by the independent variable (x-coordinate) value before processing them further.
2 comentarios
Star Strider
el 29 de Mzo. de 2021
Rosie Answorth’s Answer became this Comment —
Hi,
I have tired flipud instead of fliplr but the shaded area on my graph is still not showing up. Do you have any ideas what might be causing this? Thank you in advance
meanpt9sc=nanmean(potemp1_all(:,1:23),2)
means9sc=nanmean(salin1_all(:,1:23),2)
%standard deviation
sdpt9sc=nanstd(potemp1_all(:,1:23)')
sds9sc=nanstd(salin1_all(:,1:23)')
%plotting lines mean plus standard deviation and mean minus standard
%deviation
plot(s_std19sc,pt_std19sc,'g','Linestyle','--') %plus one standard deviation
hold on
plot(s_std29sc,pt_std29sc,'b','Linestyle','--') %-1std
%add fill between standard deviation lines
inBetween=[pt_std19sc,flipud(pt_std29sc)];
xinBetween=[s_std19sc,flipud(s_std29sc)];
fill(xinBetween,inBetween,'r','Facealpha',0.2)
%add mean line in red
hold on
plot(means9sc,meanpt9sc,'r')

Star Strider
el 29 de Mzo. de 2021
It may be necessary to upload (attach) the data, preferably as a .mat file, so I can work with it.
Note — It is necessary to vertically concatenate the vectors, not norizontally concatenate them. Replace the commas (,) with semicolons (;):
inBetween=[pt_std19sc,flipud(pt_std29sc)];
↑ ← HERE
xinBetween=[s_std19sc,flipud(s_std29sc)];
↑ ← AND HERE
as I originally suggested, and it should work.
If it still has problems, please upload your data.
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!