Boxplot multiline labels with latex interpreter
조회 수: 16 (최근 30일)
이전 댓글 표시
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
Walter Roberson
2022년 7월 18일
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');
채택된 답변
Voss
2022년 7월 17일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!