reorder on a plot

16 次查看(过去 30 天)
MR2022
MR2022 2022-1-12
编辑: MR2022 2022-1-14
vComplDire contains randomly on each cell one of these 4 directions:
DownRight; DownLeft; UpRight; UpLeft. (these are simply strings).
vComplDire is loaded by this for loop:
for i = 1:numel(vInterv)
if vbHori(i)
vComplDire{i} = 'Up';
else
vComplDire{i} = 'Down';
end
if vbVert(i)
vComplDire{i} = [vComplDire{i} 'Right'];
else
vComplDire{i} = [vComplDire{i} 'Left'];
end
end
boxplot(vInterv, vComplDire);
Boxplotting I get the graphic that I show on the attached pic.
I want to reorder the boxes on the graph like this:
1st DownLeft; 2nd DownRight; 3rd UpLeft; 4th UpRight.
I tried with "reorderlevels", "getlevels", etc... but no success.

采纳的回答

Voss
Voss 2022-1-12
% Generate random data. In order to be able to distinguish the boxes, make
% UpRight have mean 0, DownRight have mean 1, DownLeft have mean 2, and UpLeft have mean 3.
vInterv = randn(100,4)+[0 1 2 3];
vbHori = [true false false true];
vbVert = [true true false false];
vComplDire = cell(1,4);
for i = 1:4
if vbHori(i)
vComplDire{i} = 'Up';
else
vComplDire{i} = 'Down';
end
if vbVert(i)
vComplDire{i} = [vComplDire{i} 'Right'];
else
vComplDire{i} = [vComplDire{i} 'Left'];
end
end
vComplDire
vComplDire = 1×4 cell array
{'UpRight'} {'DownRight'} {'DownLeft'} {'UpLeft'}
[~,idx] = ismember({'DownLeft','DownRight','UpLeft','UpRight'},vComplDire)
idx = 1×4
3 2 4 1
boxplot(vInterv(:,idx), vComplDire(idx));
  4 个评论
MR2022
MR2022 2022-1-13
编辑:MR2022 2022-1-14
As you created it,
having "vInterv = randn(100,4)+[0 1 2 3]; ... etc."
your script works spotless. Fine.
But with my own data it doesn't work.
I probably should have included these details:
vInterv = 1x100 double
vbHori = 1x4 logical
vbVert = 1x4 logical
vComplDire = 1x100 cell (which will include these strings "DownLeft"... etc.)
vInterv has a value on each position, so "for i = 1:4" doesn't work.
I tried your code exactly this way:
for i = 1:numel(vInterv) % this nr. is 100
if vbHori(i)
vComplDire{i} = 'Up';
else
vComplDire{i} = 'Down';
end
if vbVert(i)
vComplDire{i} = [vComplDire{i} 'Right'];
else
vComplDire{i} = [vComplDire{i} 'Left'];
end
end
[~,idx] = ismember({'DownLeft','DownRight','UpLeft','UpRight'},vComplDire);
boxplot(vInterv(:,idx), vComplDire(idx));
Voss
Voss 2022-1-13
When i == 5 you will get an error in the for loop because vbHori only has 4 elements.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by