Boxplot multiline labels with latex interpreter

I am trying to plot a boxplot for data with two different lines for each label that are strings constructed from values of a matrix.
tol_r_f=[0 .2;0 .04;0 .03;.1 1];
tol_v_f=[.3 1;.09 .2;0 .07;1 10];
range={{[num2str(tol_r_f(1,1)) '$\leq ||\boldmath{r}_f|| \leq$' num2str(tol_r_f(1,2))];...
[num2str(tol_v_f(1,1)) '$\leq ||\boldmath{v}_f|| \leq$' num2str(tol_v_f(1,2))]},...
{[num2str(tol_r_f(2,1)) '$\leq ||\boldmath{r}_f|| \leq$' num2str(tol_r_f(2,2))];...
[num2str(tol_v_f(2,1)) '$\leq ||\boldmath{v}_f|| \leq$' num2str(tol_v_f(2,2))]},...
{[num2str(tol_r_f(3,1)) '$\leq ||\boldmath{r}_f|| \leq$' num2str(tol_r_f(3,2))];...
[num2str(tol_v_f(3,1)) '$\leq ||\boldmath{v}_f|| \leq$' num2str(tol_v_f(3,2))]},...
{[num2str(tol_r_f(4,1)) '$\leq ||\boldmath{r}_f|| \leq$' num2str(tol_r_f(4,2))];...
[num2str(tol_v_f(4,1)) '$\leq ||\boldmath{v}_f|| \leq$' num2str(tol_v_f(4,2))]}};
x_t1=rand(1,50)
x_t1 = 1×50
0.5273 0.9328 0.6801 0.4267 0.6459 0.8525 0.7273 0.3672 0.5781 0.0356 0.1051 0.0179 0.6041 0.2366 0.9928 0.5229 0.1017 0.5249 0.2572 0.1435 0.5885 0.1182 0.9746 0.3242 0.4079 0.7173 0.5745 0.2048 0.5527 0.5067
g_t1=[zeros(1,10) ones(1,20) 2*ones(1,10) 3*ones(1,10)];
x_t2=rand(1,50)
x_t2 = 1×50
0.6849 0.9181 0.4210 0.6358 0.5137 0.0796 0.9922 0.4163 0.8720 0.1286 0.6509 0.0395 0.4785 0.8855 0.3920 0.6896 0.0298 0.3656 0.8714 0.6152 0.5660 0.6087 0.6257 0.6035 0.4015 0.1026 0.9413 0.0172 0.3658 0.4015
g_t2=[zeros(1,10) ones(1,20) 2*ones(1,10) 3*ones(1,10)];
x_tf=rand(1,50)
x_tf = 1×50
0.4853 0.2465 0.0762 0.1879 0.7779 0.6500 0.3738 0.0251 0.5201 0.2227 0.7811 0.6906 0.7261 0.1017 0.6216 0.6077 0.6257 0.1733 0.4016 0.5364 0.4210 0.3506 0.0940 0.6231 0.2136 0.2247 0.8545 0.9760 0.5769 0.7037
g_tf=[zeros(1,10) ones(1,20) 2*ones(1,10) 3*ones(1,10)];
figure(1)
boxplot(x_t1,g_t1,'labels',range)
Error using boxplot>assignUserLabels
There must be the same number of labels as groups or as the number of elements in X.

Error in boxplot>identifyGroups (line 1254)
assignUserLabels(labels,groupIndexByPoint,numFlatGroups,xlen,...

Error in boxplot (line 290)
identifyGroups (gDat,grouporder,positions,colorgroup,...
xlabel('Cluster Ranges','Interpreter','Latex')
ylabel('$\% t_1$','Interpreter','Latex')
figure(2)
boxplot(x_t2,g_t2,'labels',range)
xlabel('Cluster Ranges','Interpreter','Latex')
ylabel('$\% t_2$','Interpreter','Latex')
figure(3)
boxplot(x_tf,g_tf,'labels',range)
xlabel('Cluster Ranges','Interpreter','Latex')
ylabel('$\% t_f$','Interpreter','Latex')

3 commentaires

You have four entries in range but there are only three unique g_tf entries. The number of unique group entries must match the number of entries in range.
@Walter Roberson my mistake. It nevertheless causes an error.
tol_r_f=[0 .2;0 .04;0 .03;.1 1];
tol_v_f=[.3 1;.09 .2;0 .07;1 10];
range={{[num2str(tol_r_f(1,1)) '$\leq ||\boldmath{r}_f|| \leq$' num2str(tol_r_f(1,2))];...
[num2str(tol_v_f(1,1)) '$\leq ||\boldmath{v}_f|| \leq$' num2str(tol_v_f(1,2))]},...
{[num2str(tol_r_f(2,1)) '$\leq ||\boldmath{r}_f|| \leq$' num2str(tol_r_f(2,2))];...
[num2str(tol_v_f(2,1)) '$\leq ||\boldmath{v}_f|| \leq$' num2str(tol_v_f(2,2))]},...
{[num2str(tol_r_f(3,1)) '$\leq ||\boldmath{r}_f|| \leq$' num2str(tol_r_f(3,2))];...
[num2str(tol_v_f(3,1)) '$\leq ||\boldmath{v}_f|| \leq$' num2str(tol_v_f(3,2))]},...
{[num2str(tol_r_f(4,1)) '$\leq ||\boldmath{r}_f|| \leq$' num2str(tol_r_f(4,2))];...
[num2str(tol_v_f(4,1)) '$\leq ||\boldmath{v}_f|| \leq$' num2str(tol_v_f(4,2))]}};
x_t1=rand(1,50)
x_t1 = 1×50
0.6093 0.4143 0.5604 0.7817 0.3154 0.6429 0.1901 0.5669 0.1163 0.0915 0.7984 0.1807 0.5601 0.6392 0.3546 0.4548 0.2190 0.1976 0.4390 0.6179 0.1811 0.5870 0.1348 0.3278 0.3646 0.0745 0.0246 0.7225 0.3311 0.8272
g_t1=[zeros(1,10) ones(1,20) 2*ones(1,10) 3*ones(1,10)];
x_t2=rand(1,50)
x_t2 = 1×50
0.9798 0.9563 0.5104 0.9247 0.7734 0.5076 0.4018 0.1795 0.3879 0.9752 0.5172 0.4234 0.7161 0.0341 0.9010 0.2039 0.1041 0.6151 0.1234 0.1109 0.2811 0.6940 0.6776 0.8558 0.3473 0.5074 0.2569 0.1797 0.3170 0.1876
g_t2=[zeros(1,10) ones(1,20) 2*ones(1,10) 3*ones(1,10)];
x_tf=rand(1,50)
x_tf = 1×50
0.4488 0.7155 0.4695 0.2569 0.2076 0.9335 0.3564 0.3334 0.8679 0.7221 0.5543 0.9747 0.0139 0.4763 0.5383 0.9334 0.4048 0.9509 0.2022 0.9712 0.9588 0.2008 0.0739 0.8498 0.3509 0.2855 0.8247 0.1169 0.5238 0.8712
g_tf=[zeros(1,10) ones(1,20) 2*ones(1,10) 3*ones(1,10)];
figure(1)
Range1 = cellfun(@(C) C{1}, range, 'UniformOutput',false);
Range2 = cellfun(@(C) C{2}, range, 'UniformOutput',false);
Range = {Range1, Range2};
boxplot(x_t1, g_t1.', 'labels', Range)
xlabel('Cluster Ranges','Interpreter','Latex')
ylabel('$\% t_1$','Interpreter','Latex')
set(findobj(1, 'type', 'text'), 'interpreter', 'latex');
figure(2)
boxplot(x_t2, g_t2.', 'labels', Range)
xlabel('Cluster Ranges','Interpreter','Latex')
ylabel('$\% t_2$','Interpreter','Latex')
set(findobj(2, 'type', 'text'), 'interpreter', 'latex');
figure(3)
boxplot(x_tf, g_tf.', 'labels', Range)
xlabel('Cluster Ranges','Interpreter','Latex')
ylabel('$\% t_f$','Interpreter','Latex')
set(findobj(3, 'type', 'text'), 'interpreter', 'latex');

Connectez-vous pour commenter.

 Réponse acceptée

I couldn't get it to work either, but here's a workaround where you create text objects instead of setting the xticklabels (shown for the first figure only):
tol_r_f=[0 .2;0 .04;0 .03;.1 1];
tol_v_f=[.3 1;.09 .2;0 .07;1 10];
range = sprintfc([ ...
'%g$\\leq ||\\boldmath{r}_f|| \\leq$%g\n' ...
'%g$\\leq ||\\boldmath{v}_f|| \\leq$%g'], ...
[tol_r_f, tol_v_f])
range = 4×1 cell array
{'0$\leq ||\boldmath{r}_f|| \leq$0.2↵0.3$\leq ||\boldmath{v}_f|| \leq$1' } {'0$\leq ||\boldmath{r}_f|| \leq$0.04↵0.09$\leq ||\boldmath{v}_f|| \leq$0.2'} {'0$\leq ||\boldmath{r}_f|| \leq$0.03↵0$\leq ||\boldmath{v}_f|| \leq$0.07' } {'0.1$\leq ||\boldmath{r}_f|| \leq$1↵1$\leq ||\boldmath{v}_f|| \leq$10' }
x_t1=rand(1,50);
g_t1=[zeros(1,10) ones(1,20) 2*ones(1,10) 3*ones(1,10)];
x_t2=rand(1,50);
g_t2=[zeros(1,10) ones(1,20) 2*ones(1,10) 3*ones(1,10)];
x_tf=rand(1,50);
g_tf=[zeros(1,10) ones(1,20) 2*ones(1,10) 3*ones(1,10)];
figure(1)
boxplot(x_t1,g_t1)%,'labels',range)
yl = ylim();
yt = yl(1)-0.02*(yl(2)-yl(1));
xt = xticks();
nt = numel(xt);
xticklabels(repmat({''},1,nt));
text(xt,yt*ones(1,nt),range, ...
'Interpreter','latex', ...
'HorizontalAlignment','center', ...
'VerticalAlignment','top')
xlabel(sprintf('\n\n\nCluster Ranges'),'Interpreter','Latex')
ylabel('$\% t_1$','Interpreter','Latex')

Plus de réponses (0)

Catégories

En savoir plus sur Data Distribution Plots 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!

Translated by