Create box chart with only Min Max Mean and Standard Deviation values
Afficher commentaires plus anciens
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 !
Réponse acceptée
Plus de réponses (1)
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)
Catégories
En savoir plus sur Smoothing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

