How to plot XScale in log in boxplot? I used set(gca,'XScale','log') but the scale width are unequal. Kindly help.
26 次查看(过去 30 天)
显示 更早的评论
I want the xscale to appear as below from 10^-1 to 10^1 with equal spacing but unable to achieve it with the following code.
CODE:
clear all;
close all;
C0p1 = [91.57 89.96 86.61]'; %for 0.1m transmission distance 10, 150, 1000 mS/m
C0p2=[78.42 75.22 68.51]';
C0p3 = [68.76 63.95 53.89]';
C0p4=[61.3 54.89 41.48]';
C0p5 = [55.25 47.23 30.47]';
C0p6=[50.15 40.52 20.41]'
C0p7=[45.73 34.5 11.03]'
C0p8=[41.81 28.97 2.156]'
C0p9=[38.28 23.84 -6.326]'
C1 = [35.0707 19.0265 -14.495]'; %No need to change 1 to 5
C2=[12.1437 -19.9448 -86.9877]';
C3 = [-3.3613 -51.4904 -152.0584]';
C4=[-15.8081 -79.9851 -214.071]';
C5 = [-26.5764 -106.7977 -274.405]';
C6=[-36.28 -132.5 -333.7]'
C7=[-45.25 -157.6 -392.2]'
C8=[-53.69 -182 -450.2]'
C9=[-61.72 -206.1 -507.8]'
C10=[-69.42 -229.9 -565.1]'
group = [ ones(size(C0p1));
2 * ones(size(C0p2));
3 * ones(size(C0p3))
4 * ones(size(C0p4))
5 * ones(size(C0p5))
6 * ones(size(C0p6));
7 * ones(size(C0p7))
8 * ones(size(C0p8))
9 * ones(size(C0p9))
10 * ones(size(C1));
11 * ones(size(C2));
12 * ones(size(C3))
13 * ones(size(C4))
14 * ones(size(C5))
15 * ones(size(C6));
16 * ones(size(C7))
17 * ones(size(C8))
18 * ones(size(C9))
19 * ones(size(C10))];
group = repelem((1:19)',[numel(C0p1);numel(C0p2);numel(C0p3);numel(C0p4);numel(C0p5);numel(C0p6);numel(C0p7);numel(C0p8);numel(C0p9);numel(C1);numel(C2);numel(C3);numel(C4);numel(C5);numel(C6);numel(C7);numel(C8);numel(C9);numel(C10)]);
h1=boxplot([C0p1; C0p2; C0p3; C0p4; C0p5; C0p6; C0p7; C0p8; C0p9; C1; C2; C3; C4; C5; C6; C7; C8; C9; C10],group,'colors','g','widths',0.25,'BoxStyle','filled')
set(gca,'XTickLabel',{'0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'})
lines = findobj(h1, 'type', 'line', 'Tag', 'Median');
set(lines, 'Color', 'R');
------------------------------------------
For: set(gca,'XTickLabel',{'0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'})
Output is:
For: set(gca,'XScale','log')
Output is:
For: set(gca,'Xtick',-1:1); %// adjust manually; values in log scale
set(gca,'Xticklabel',10.^get(gca,'Xtick'));
Output is:
0 个评论
回答(1 个)
Asvin Kumar
2021-6-23
You were right to set the 'XScale' axes property to 'log'. The reason it didn't behave as expected was becasuse the positions of each box in the boxplot were 1:19 by default. You can modify that by using the 'Position' Name-Value pair. Here's an updated version of the code.
C0p1 = [91.57 89.96 86.61]'; %for 0.1m transmission distance 10, 150, 1000 mS/m
C0p2=[78.42 75.22 68.51]';
C0p3 = [68.76 63.95 53.89]';
C0p4=[61.3 54.89 41.48]';
C0p5 = [55.25 47.23 30.47]';
C0p6=[50.15 40.52 20.41]';
C0p7=[45.73 34.5 11.03]';
C0p8=[41.81 28.97 2.156]';
C0p9=[38.28 23.84 -6.326]';
C1 = [35.0707 19.0265 -14.495]'; %No need to change 1 to 5
C2=[12.1437 -19.9448 -86.9877]';
C3 = [-3.3613 -51.4904 -152.0584]';
C4=[-15.8081 -79.9851 -214.071]';
C5 = [-26.5764 -106.7977 -274.405]';
C6=[-36.28 -132.5 -333.7]';
C7=[-45.25 -157.6 -392.2]';
C8=[-53.69 -182 -450.2]';
C9=[-61.72 -206.1 -507.8]';
C10=[-69.42 -229.9 -565.1]';
pos = [0.1:0.1:0.9 1:10];
group = repelem((pos)', ... % updated the group numbers
[numel(C0p1);numel(C0p2);numel(C0p3);numel(C0p4);numel(C0p5);...
numel(C0p6);numel(C0p7);numel(C0p8);numel(C0p9);numel(C1);numel(C2);...
numel(C3);numel(C4);numel(C5);numel(C6);numel(C7);numel(C8);numel(C9);...
numel(C10)]);
x = [C0p1; C0p2; C0p3; C0p4; C0p5; C0p6; C0p7; C0p8; C0p9; C1; C2; C3; C4; ...
C5; C6; C7; C8; C9; C10];
h1=boxplot(x,group,'colors','g','widths',0.25,...
'BoxStyle','filled','Positions',pos); % updated positions
lines = findobj(h1, 'type', 'line', 'Tag', 'Median');
set(lines, 'Color', 'R');
grid on;
ax = gca;
ax.XScale ='log';
ax.XTickLabelMode = 'auto'; % optional
ax.XTickMode = 'auto'; % optional
You will notice that the code throws a warning. This warning is because the position of the Median of the first bar plot has negative values which cannot be converted to a log scale.
You will also notice that the markers have a decreasing length from left to right. This is also because of the conversion from a linear to a log scale. All Median lines have the same length in the linear scale but it looks different in the log scale. This can be fixed by updating the 'XData' property of all lines with the 'Median' tag.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Point Cloud Processing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!