Boxplot multiline labels with latex interpreter
16 views (last 30 days)
Show older comments
Alessandro Maria Laspina
on 17 Jul 2022
Commented: Walter Roberson
on 18 Jul 2022
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)
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)
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 Comments
Walter Roberson
on 18 Jul 2022
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)
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)
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');
Accepted Answer
Voss
on 17 Jul 2022
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])
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')
0 Comments
More Answers (0)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!