Create box chart with only Min Max Mean and Standard Deviation values

77 次查看(过去 30 天)
Hi everyone,
From an external software, I have the min, max, mean, and standard deviation values of a large set of data. Can I create a box chart with only these values ? From I have seen, boxchart requires the entire data to compute by itslef these values. But I cannot make it work like this.
Maybe the best should be to create a new data that respects the min, max, mean and standard deviation that I want, and feed the boxchart function with this new data but I have not found any way to create data that respects all four parameters at once.
In the end my goal is to create a function with (min, max, mean, std) as inputs, that gives in the output a box chart.
Thank you for your help !

采纳的回答

Valentin POUZET
Valentin POUZET 2023-2-16
Damn, I've posted the solution in the comment of somebody else's answer. Then he deleted his answer, and my comment with. I cannot export the 1st and 3rd quartiles from the software that the values come from, only the std, as well as I cannot export the median, but only the average. So I am looking for replacing the 1st and 3rd quartiles by the average+/-std in the graph. After using the help of ChatGPT, I could have the following solution:
min_val = 1.5;
median_val = 2.2; %Here is the average from my external software
std_val=0.19; %Here is the std from my external software
q1_val = median_val-std_val;
q3_val = median_val+std_val;
max_val = 2.5;
figure
%There is twice the median value so the box will perfectly fit the quartile values
boxplot([min_val, q1_val, median_val, median_val, q3_val, max_val], 'Labels', {'Data'}, 'BoxStyle', 'outline', 'Whisker', 1.5, 'Positions', 1, 'Symbol', 'o', 'Colors', 'b', 'Widths', 0.5, 'Notch', 'off', 'MedianStyle', 'line');
ylim([1.4 2.9])
grid
This way, I have the following graph:
This is exactly what I was looking for. And this works with boxchart too.
Sorry @Voss that you waste time to investigate on my issue whereas I already found the solution. Anyway, thank you for your help !

更多回答(1 个)

Voss
Voss 2023-2-16
I don't know of a way to explicitly specify the min, max, mean and std of a data set in boxchart (or boxplot). (Note that those use median and 25th and 75th percentiles anyway - not sure there's a way to change that either.)
Your idea of generating a dataset with the required characteristics could work (except that boxchart/boxplot would still use median, 25th, 75th percentile instead of mean, mean-std, mean+std).
An alternative would be to create your own depiction using primitive graphics objects (lines and patches, in this case).
Here's an example:
% number of data sets for which we have min, max, mean, std
Npts = 8;
% generate random data.
% this variable is not used except to calculate the min, max, mean, std
data = randn(100,Npts);
% calculate the min, max, mean, std.
% these are what are used in the remaining calculations.
data_min = min(data,[],1);
data_max = max(data,[],1);
data_mean = mean(data,1);
data_std = std(data,1);
% --- you could make the below code into a function ---
Npts = numel(data_min);
box_width = 0.5;
dx = box_width/2;
% vertical line x- and y-coordinates
v_x = [1:Npts; 1:Npts; NaN(1,Npts)];
v_y = [data_min; data_max; NaN(1,Npts)];
% horizontal line (at min and max) x- and y-coordinates
h_x = (1:Npts)+[-dx; dx; NaN; -dx; dx; NaN];
h_y = [data_min([1 1],:); NaN(1,Npts); data_max([1 1],:); NaN(1,Npts)];
% patch (mean-std to mean+std) x- and y-coordinates
p_x = (1:Npts)+[-dx; dx];
p_x = p_x([1 1 2 2 1],:);
p_y = data_mean+[-data_std; data_std];
p_y = p_y([1 2 2 1 1],:);
% mean line (red) x- and y-coordinates
m_x = (1:Npts)+[-dx; dx; NaN];
m_y = [data_mean([1 1],:); NaN(1,Npts)];
figure
line(v_x(:),v_y(:),'Color','k','LineStyle','--')
line(h_x(:),h_y(:),'Color','k','LineStyle','-')
patch(p_x,p_y,'b')
line(m_x(:),m_y(:),'Color','r','LineWidth',1.5)

类别

Help CenterFile Exchange 中查找有关 Smoothing 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by