plot standard deviation as a shaded area
306 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hey,
I have a vector containing the mean values and a another vector with the standard deviations. I want to plot the standard deviation as a shaded area and the mean as a line as shown on the image below but I want to write my own function. hope someone can help

Antworten (1)
M
am 3 Dez. 2019
Bearbeitet: M
am 3 Dez. 2019
You can use the code directly from this answer:
y = rand(1,10); % your mean vector;
x = 1:numel(y);
std_dev = 1;
curve1 = y + std_dev;
curve2 = y - std_dev;
x2 = [x, fliplr(x)];
inBetween = [curve1, fliplr(curve2)];
fill(x2, inBetween, 'g');
hold on;
plot(x, y, 'r', 'LineWidth', 2);
2 Kommentare
Baldvin
am 14 Nov. 2025 um 17:44
Here's the a similar code as above, using Patch and its properties. Hopefully, the variable names are descriptive:
% Dummy data:
x = linspace(-2,2,64);
meanCurve = 2 + x.^2 + 0.55*rand(size(x));
upperCurve = meanCurve + 0.5*abs(randn(size(x))) + 1;
lowerCurve = meanCurve - 0.5*abs(randn(size(x))) - 1;
% Create shaded area. We just have to create x/y points that trace out the
% bottom curve from left to right, followed by the upper curve from right to left:
areaX = [x, fliplr(x)];
areaY = [lowerCurve, fliplr(upperCurve)];
shadedArea = patch("XData",areaX, "YData",areaY, ...
"FaceColor",'flat',"EdgeColor",'flat',"SeriesIndex",2, ...
"FaceAlpha",0.2, "EdgeAlpha",1);
% Add the curve:
hold on
meanLine = plot(x, meanCurve, "SeriesIndex",2, "LineWidth", 2);
Star Strider
am 14 Nov. 2025 um 18:29
This works for row vectors:
areaX = [x, flip(x)];
areaY = [lowerCurve, flip(upperCurve)];
For column vectors, use:
areaX = [x; flip(x)];
areaY = [lowerCurve; flip(upperCurve)];
NOTE -- The flip function will flip a vector appropriately. Use flipud or fliplr if you are working with matrices. (Using fliplr on a column vector does nothing useful!)
.
Siehe auch
Kategorien
Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
