I want to move up my boxchart

9 visualizzazioni (ultimi 30 giorni)
Chiel van Wanrooij
Chiel van Wanrooij il 4 Gen 2023
Commentato: Voss il 4 Gen 2023
I want to move up my boxchart a little bit. I want it to have two boxes for 0min right next to eachother, also for the other 3 exposure times right next to eachother. So, you will still have 4 catogories, but for every catagorie two boxes next to eachother. Because, it is a little bit hard to see now. Thank you!
%% Glass Ra
clear all, close all;
glass = xlsread('excelsensofarglass')
glass2 = xlsread('excelsensofarglass2')
glass3 = xlsread('excelsensofarglass3')
glass4 = xlsread('excelsensofarglass4')
glass5 = xlsread('excelsensofarglass5')
glass6 = xlsread('excelsensofarglass6')
G1 = [transpose(glass(1:4,1));transpose(glass2(1:4,1));transpose(glass3(1:4,1));transpose(glass4(1:4,1));transpose(glass5(1:4,1));transpose(glass6(1:4,1))]
G2 = [transpose(glass(6:9,1));transpose(glass2(6:9,1));transpose(glass3(6:9,1));transpose(glass4(6:9,1));transpose(glass5(6:9,1));transpose(glass6(6:9,1))]
G4 = [transpose(glass(11:14,1));transpose(glass2(11:14,1));transpose(glass3(11:14,1));transpose(glass4(11:14,1));transpose(glass5(11:14,1));transpose(glass6(11:14,1))]
G6 = [transpose(glass(16:19,1));transpose(glass2(16:19,1));transpose(glass3(16:19,1));transpose(glass4(16:19,1));transpose(glass5(16:19,1));transpose(glass6(16:19,1))]
figure(1)
hold on
% boxchart(G1)
boxchart(G2)
boxchart(G4)
% boxchart(G6)
ylim([0 1.6])
ylabel('Arithmetic average height [\mum]')
xlabel('duration of exposure')
title('Arithmetic average height glass')
legend('20 [\mum]','40 [\mum]','location','northwest')
set(gca,'XTickLabel',{'0min','60min','90min','240min'})

Risposta accettata

Voss
Voss il 4 Gen 2023
Modificato: Voss il 4 Gen 2023
You can create a single boxchart per boxchart call, with a single column of data, and set its X location as desired.
To illustrate: first, the original, showing the overlapping boxcharts (using random data):
% Some random data:
G2 = rand(6,4);
G4 = rand(6,4);
figure%(1)
hold on
% boxchart(G1)
boxchart(G2)
boxchart(G4)
% boxchart(G6)
ylim([0 1.6])
ylabel('Arithmetic average height [\mum]')
xlabel('duration of exposure')
title('Arithmetic average height glass')
legend('20 [\mum]','40 [\mum]','location','northwest')
set(gca,'XTickLabel',{'0min','60min','90min','240min'})
Now, with multiple boxchart calls:
figure%(1)
hold on
[n_rows,n_cols] = size(G2);
x = zeros(n_rows,1);
colors = get(gca(),'ColorOrder');
for ii = 1:n_cols
boxchart(x+ii-0.15,G2(:,ii),'BoxWidth',0.3,'BoxFaceColor',colors(1,:))
boxchart(x+ii+0.15,G4(:,ii),'BoxWidth',0.3,'BoxFaceColor',colors(2,:))
end
ylim([0 1.6])
ylabel('Arithmetic average height [\mum]')
xlabel('duration of exposure')
title('Arithmetic average height glass')
legend('20 [\mum]','40 [\mum]','location','northwest')
set(gca, ...
'XTick',1:4, ...
'XTickLabel',{'0min','60min','90min','240min'})
  5 Commenti
Voss
Voss il 4 Gen 2023
See below for a version with 4 groups:
% Some random data:
G1 = rand(6,4);
G2 = rand(6,4);
G4 = rand(6,4);
G6 = rand(6,4);
figure%(1)
hold on
[n_rows,n_cols] = size(G2);
x = zeros(n_rows,1);
colors = get(gca(),'ColorOrder');
x_offset = [-0.3 -0.1 0.1 0.3];
box_width = 0.2;
for ii = 1:n_cols
boxchart(x+ii+x_offset(1),G1(:,ii),'BoxWidth',box_width,'BoxFaceColor',colors(1,:))
boxchart(x+ii+x_offset(2),G2(:,ii),'BoxWidth',box_width,'BoxFaceColor',colors(2,:))
boxchart(x+ii+x_offset(3),G4(:,ii),'BoxWidth',box_width,'BoxFaceColor',colors(3,:))
boxchart(x+ii+x_offset(4),G6(:,ii),'BoxWidth',box_width,'BoxFaceColor',colors(4,:))
end
ylim([0 1.6])
ylabel('Arithmetic average height [\mum]')
xlabel('duration of exposure')
title('Arithmetic average height glass')
legend('10 [\mum]','20 [\mum]','40 [\mum]','60 [\mum]','location','northwest')
set(gca, ...
'XTick',1:4, ...
'XTickLabel',{'0min','60min','90min','240min'})
Voss
Voss il 4 Gen 2023
Here's a more general version.
You can change data, group_number_for_legend, and/or column_number_for_xticks, and the rest of the code will work as long as column_number_for_xticks has the same number of elements as data has pages (i.e., the number of matrices G1, G2, G4, ... used in data), and column_number_for_xticks has the same number of elements as each matrix has columns.
As they are defined here below, you get 5 x-ticks, each with a set of 6 boxcharts.
% Some random data, 6 matrices of size 10-by-5:
G1 = rand(10,5);
G2 = rand(10,5);
G4 = rand(10,5);
G6 = rand(10,5);
G9 = rand(10,5);
G11 = rand(10,5);
% concatenate all the data in a 3D array:
data = cat(3,G1,G2,G4,G6,G9,G11);
% numbers to display in the legend:
group_number_for_legend = [10 20 40 60 90 110];
% numbers to display in the xticklabels:
column_number_for_xticks = [0 60 90 240 420];
figure%(1)
hold on
[n_rows,n_cols,n_groups] = size(data);
colors = get(gca(),'ColorOrder');
colors = repmat(colors,ceil(n_groups/size(colors,1)),1);
box_width = 1/(n_groups+1);
if n_groups == 1
x_offset = 0;
else
x_offset = linspace(-0.5+box_width,0.5-box_width,n_groups);
end
x = zeros(n_rows,1);
for ii = 1:n_cols
for kk = 1:n_groups
boxchart(x+ii+x_offset(kk),data(:,ii,kk),'BoxWidth',box_width,'BoxFaceColor',colors(kk,:))
end
end
ylim([0 1.6])
ylabel('Arithmetic average height [\mum]')
xlabel('duration of exposure')
title('Arithmetic average height glass')
legend(sprintfc('%d [\\mum]',group_number_for_legend),'location','northwest')
set(gca, ...
'XTick',1:n_cols, ...
'XTickLabel',sprintfc('%dmin',column_number_for_xticks))
Another run with a different setup, to create 4 sets of 3 boxcharts:
% Some random data, 3 matrices of size 10-by-4:
G1 = rand(10,4);
G2 = rand(10,4);
G4 = rand(10,4);
% concatenate all the data in a 3D array:
data = cat(3,G1,G2,G4);
% numbers to display in the legend:
group_number_for_legend = [10 20 40];
% numbers to display in the xticklabels:
column_number_for_xticks = [0 60 90 240];
figure%(1)
hold on
[n_rows,n_cols,n_groups] = size(data);
colors = get(gca(),'ColorOrder');
colors = repmat(colors,ceil(n_groups/size(colors,1)),1);
box_width = 1/(n_groups+1);
if n_groups == 1
x_offset = 0;
else
x_offset = linspace(-0.5+box_width,0.5-box_width,n_groups);
end
x = zeros(n_rows,1);
for ii = 1:n_cols
for kk = 1:n_groups
boxchart(x+ii+x_offset(kk),data(:,ii,kk),'BoxWidth',box_width,'BoxFaceColor',colors(kk,:))
end
end
ylim([0 1.6])
ylabel('Arithmetic average height [\mum]')
xlabel('duration of exposure')
title('Arithmetic average height glass')
legend(sprintfc('%d [\\mum]',group_number_for_legend),'location','northwest')
set(gca, ...
'XTick',1:n_cols, ...
'XTickLabel',sprintfc('%dmin',column_number_for_xticks))

Accedi per commentare.

Più risposte (2)

Cameron
Cameron il 4 Gen 2023
I'm not sure if there is a way to do that. You can always use this instead.
% Left axes
ax1 = nexttile;
boxchart(ax1,G2)
set(ax1,'XTickLabel',{'0min','60min','90min','240min'});
% Right axes
ax2 = nexttile;
boxchart(ax2,G4)
set(ax2,'XTickLabel',{'0min','60min','90min','240min'});

the cyclist
the cyclist il 4 Gen 2023
It doesn't seem to me that the code you pasted could create the plot example you've posted (but maybe I'm misunderstanding something). Can you upload your G1, G2, ... data? (You can use the paper clip icon in the INSERT section of the toolbar.)
Without understanding more, my best suggestion would be instead of using the
boxchart(ydata)
syntax, use
boxchart(xgroupdata,ydata)
and give the two groups you want to separate slightly different x values (e.g. [-1,1], [-59 61], etc), so that they are offset from each other.

Categorie

Scopri di più su Elementary Polygons in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by