Line graph shading in matlab
15 views (last 30 days)
Show older comments
Hello! My name is Fran. I have a doubt about line graphs shading in Matlab. I'm trying to plot a line of means in a sample in a time serie and would like to plot the standard deviation on it, by means of shadows, but I don´t know how to do it. I know the error bars plot, but I would like to use shadows if is possible. I hope you can help me with this question.
Thank you very much.
0 Comments
Answers (1)
ag
on 12 Mar 2025
Hi Francisco,
To add shaded regions representing the standard deviation around a line plot in MATLAB, you can use the "fill" function to create a shaded area between the mean ± standard deviation.
The below code demonstrates how you can achieve this:
% Sample data
t = 0:0.1:10;
meanValues = sin(t);
stdDev = 0.2;
% Calculate the upper and lower bounds
upperBound = meanValues + stdDev;
lowerBound = meanValues - stdDev;
% Plot the shaded area
figure;
hold on;
% Use 'fill' to create the shaded area
fill([t, fliplr(t)], [upperBound, fliplr(lowerBound)], [0.9, 0.9, 0.9], 'LineStyle', 'none');
% Plot the mean line
plot(t, meanValues, 'b', 'LineWidth', 2);
xlabel('Time');
ylabel('Value');
legend('Standard Deviation', 'Mean');
hold off;
For more details, please refer to the following MathWorks documentation: https://www.mathworks.com/help/matlab/ref/fill.html
Hope this helps!
0 Comments
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!