plotting multiple boxplots in the same figure window

I have 10 vectors of temperature data, all different lengths, that I want to make boxplots of and plot them all in the same figure window. How do I do this?

1 Commento

Hi Jenny,

You probably have already created a Random Temperature Data Vectors, but I am showing an example by creating random temperature data vectors. In my provided code snippet, the data vectors are generated using the randn function. You can replace these vectors with your actual temperature data. Then, create a Figure for Boxplots by create a figure to display the boxplots. The figure function is used to create a new figure window. Afterwards, plot Boxplots for Each Vector by iterating over each data vector using a loop and plot the boxplot for each vector in a subplot. Here's the modified code snippet with detailed explanations:

% Generate random temperature data vectors (replace with your actual data)

data = {randn(20,1), randn(30,1), randn(25,1), randn(35,1), randn(15,1),

randn(40,1), randn(22,1), randn(28,1), randn(18,1), randn(32,1)};

% Create a figure for boxplots

figure;

hold on;

% Iterate over each vector and plot its boxplot

for i = 1:numel(data)

    subplot(2,5,i); % Adjust subplot layout based on the number of vectors
    boxplot(data{i});
    title(['Vector ', num2str(i)]);

end

hold off;

Please see attached plot.

Feel free to customize the code further based on your specific data and visualization requirements. Hope, this answers your question.

Accedi per commentare.

 Risposta accettata

Here's how to do that with three vectors of different lengths:
x1 = rand(10,1); x2 = 2*rand(15,1); x3 = randn(30,1);
x = [x1;x2;x3];
g = [ones(size(x1)); 2*ones(size(x2)); 3*ones(size(x3))];
boxplot(x,g)

6 Commenti

How do I plot the 5th and 95th percentile as whiskers ?
How would I then label them as x1, x2, etc? Thanks!
It works!!! You're amazing Mr Tom Lane!
matlab's notation is so incredibly non user friendly it blows my mind
Just a note that as of R2020a this can also be accomplished using the boxchart function in MATLAB. boxplot is part of Statistics and Machine Learning Toolbox.

Accedi per commentare.

Più risposte (3)

Matt Learner
Matt Learner il 25 Mar 2018
Modificato: Matt Learner il 25 Mar 2018
A = [16 20 15 17 22 19 17]';
B = [22 15 16 16 16 18]';
C = [23 9 15 18 13 27 17 14 16 15 21 19 17]';
group = [ ones(size(A)); 2 * ones(size(B)); 3 * ones(size(C))];
figure
boxplot([A; B; C],group)
set(gca,'XTickLabel',{'A','B','C'})

4 Commenti

How can I change the color of A,B and C seperatly?
Did you find out how to do that? I'm still searching
@Matt Learner Thank you for your answer. I was having the same problem. For the above example they it has only three variables (A, B,C), but what will happen if it has 1000 variables, are we going to type all in the group?
I ask any robest way to do this for longer data.
You would store the variables in a structure, where each field is a variable. Then you would loop through the fields, vertically concatenating each one.
e.g.
s.A = rand(10,1);
s.B = rand(15,1);
s.C = rand(20,1);
allData = [];
allCats = [];
allFields = string(fieldnames(s))
for iField = 1:length(allFields)
allData = [allData;s.(allFields(iField))];
allCats = [allCats,repelem(allFields(iField),length(s.(allFields(iField))));]
end
boxplot(allData,allCats)

Accedi per commentare.

The question and Tom Lane's answer refers to boxplot, which is in the Statistics and Machine Learning Toolbox. Starting in R2020a, boxchart (documentation) is available in MATLAB:
A = randn(67,1);
B = randn(42,1) + 3;
C =randn(867,1)*2 + 1;
allData = [A(:);B(:);C(:)];
dataInfo = repelem(["A","B","C"],[numel(A),numel(B),numel(C)]);
boxchart(categorical(dataInfo),allData)
Note that I'm "massaging" the data into a column vector, but there are many ways to handle that as has been pointed out here. Here's another example using structs, and here we get one boxchart per vector:
s.A=A; s.B=B; s.C=C;
vectorNames=string(fieldnames(s));
f=figure;
ax=axes(f,NextPlot='add');
for i=1:numel(vectorNames)
boxchart(ax,categorical(repelem(vectorNames(i),numel(s.(vectorNames(i))))),...
s.(vectorNames(i)),"Tag",vectorNames(i))
end
I've manually placed each box at "A", "B", etc. The reason for the different colors is that each box is a separate boxchart object, but we can control this via the many properties of boxchart. I've tagged each box, and they can be easily picked out via
specificBoxchart = findobj(ax.Children,"Tag","A"); % Replace "A" with the variable/vector name etc.
Hopefully, this gives an idea of how to play around with boxchart.

Richiesto:

il 17 Nov 2012

Risposto:

il 13 Nov 2025

Community Treasure Hunt

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

Start Hunting!

Translated by