Line graph shading in matlab

15 ビュー (過去 30 日間)
Francisco Luis Sánchez Fernández
Francisco Luis Sánchez Fernández 2020 年 12 月 16 日
回答済み: ag 2025 年 3 月 12 日
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.

回答 (1 件)

ag
ag 2025 年 3 月 12 日
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!

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by