Create plot with shaded standard deviation, but ...

290 views (last 30 days)
Hello,
I have tried searching on the internet about this issue, but I haven't found what I needed, so I'm here.
I am trying to create a plot that looks like this:
There are some important things that I need to say. The standard deviation will be different in each of the points. To be more specific, I will show you my data that I actually want to plot.
data = readmatrix("data.xlsx") % matrix with 3 columns
data = 28×3
-28.1968 -29.2280 -30.2794 -28.5418 -28.5263 -28.8859 -31.8108 -28.8128 -28.4971 -30.5379 -28.4347 -28.1727 -31.6614 -29.4493 -30.4832 -30.0855 -31.9214 -29.4594 -32.7221 -30.9628 -29.3129 -29.9808 -23.2623 -29.7403 -30.5717 -29.8256 -29.8165 -28.8883 -29.0316 -30.9919
Each column contains numbers. From these numbers, an average value will and standard deviation will be calculated. Three columns, so only three values on the X axis.
Basic plot without these standard deviations would look like so:
% columns 2 and 3 are shorter, so i ignore NaN values
avg_data = [mean(data(:,1)), mean(data(~isnan(data(:,2)),2)), mean(data(~isnan(data(:,3)),3))]
avg_data = 1×3
-30.1883 -29.3129 -29.7215
std_data = [std(data(:,1)), std(data(~isnan(data(:,2)),2)), std(data(~isnan(data(:,3)),3))]
std_data = 1×3
1.3162 1.9987 0.8482
plot(1:3, avg_data)
I have all the data, but I am not aware how to create pretty plot with shaded areas. Other Mathworks Answers were usually using only one std for all the X values, but I have different std value for every X value.

Accepted Answer

Voss
Voss on 13 Mar 2023
data = readmatrix("data.xlsx") % matrix with 3 columns
data = 28×3
-28.1968 -29.2280 -30.2794 -28.5418 -28.5263 -28.8859 -31.8108 -28.8128 -28.4971 -30.5379 -28.4347 -28.1727 -31.6614 -29.4493 -30.4832 -30.0855 -31.9214 -29.4594 -32.7221 -30.9628 -29.3129 -29.9808 -23.2623 -29.7403 -30.5717 -29.8256 -29.8165 -28.8883 -29.0316 -30.9919
% columns 2 and 3 are shorter, so i ignore NaN values
avg_data = mean(data,1,'omitnan')
avg_data = 1×3
-30.1883 -29.3129 -29.7215
std_data = std(data,0,1,'omitnan')
std_data = 1×3
1.3162 1.9987 0.8482
x = 1:size(data,2);
fill([x, flip(x)], [avg_data+std_data, flip(avg_data-std_data)], [0.8 0.8 0.8])
hold on
plot(x, avg_data, 'k')

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!