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
stdshade.PNG

댓글 수: 2

M
M 2019년 12월 3일
I want to write my own function
Does this mean you do not want to use built-in function like fill or area ?
You can start by reading this discussion:
https://mathworks.com/matlabcentral/answers/180829-shade-area-between-graphs
Hinna Ahmed
Hinna Ahmed 2019년 12월 3일
I want to use built-in functions like fill or area but not functions like stdshade because they use a matrix with all the observations as a input but i have already calculated the mean and std.

댓글을 달려면 로그인하십시오.

 채택된 답변

M
M 2019년 12월 3일
편집: M 2019년 12월 3일

13 개 추천

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);

댓글 수: 4

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
Star Strider 2025년 11월 14일
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!)
.
Paige
Paige 2026년 9월 16일 17:15
편집: dpb 2026년 9월 16일 17:20
I am having similar problems with my code and I think its because my x axis is not numerical, rather a date and time.
This is the plot that was produced from the following code:
peakdir = data(:,8);
peakdirspr = data(:,9);
upperp = peakdir + peakdirspr/2;
lowerp = peakdir - peakdirspr/2;
areaX = [time, fliplr(time)];
areaY = [lowerp, fliplr(upperp)];
shadedArea = patch("XData",areaX, "YData",areaY, ...
"FaceColor",'flat',"EdgeColor",'flat',"SeriesIndex",2, ...
"FaceAlpha",0.2, "EdgeAlpha",1);
hold on
meanLine = plot(time, peakdir, "SeriesIndex",2, "LineWidth", 2);
This was code that I got from another thread, but I was exactly able to make it work. Any input would be greatly appreciated!
dpb
dpb 2026년 9월 16일 17:29
",,,I think its because my x axis is not numerical, rather a date and time."
No, the problem is that the last y data being lower in magnitude when flipped back to the origin to create the boundaries for the patch object is a straight line between the two points. The "jaggies" in the time trace are the underlying problem.
It would be helpful to attach the data file so folks have something to work with instead of having to make up simulated data to illustrate potential solutions.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

질문:

2019년 12월 3일

댓글:

dpb
2026년 9월 16일 17:29

Community Treasure Hunt

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

Start Hunting!

Translated by