Boxplot for both x and y axis with different box widths.

How can we make a boxplot like the one below for both x and y data (the box widths of each group that are x-data related are different). I tried boxplot(X,Y, group) but it did not work.
boxplot for x- and y-data

4 件のコメント

Jonas
Jonas 2021 年 4 月 30 日
編集済み: Jonas 2021 年 4 月 30 日
i think you can not specify mutiple different box widths directly, you would have to edit the box sizes manually by accesing the XData of the boxes
Benjamin Kraus
Benjamin Kraus 2021 年 4 月 30 日
boxchart does offer a setting for the box width.
Adam Danz
Adam Danz 2021 年 4 月 30 日
編集済み: Adam Danz 2021 年 4 月 30 日
I missed that since BoxWidth is not mentioned on the documentation page for boxchart, however it is listed in the properties page.
Thanks, Benjamin.
Gechang Cao
Gechang Cao 2021 年 5 月 1 日
Thanks for your nice suggestions! boxchart works

サインインしてコメントする。

 採用された回答

Benjamin Kraus
Benjamin Kraus 2021 年 4 月 30 日
編集済み: Benjamin Kraus 2021 年 4 月 30 日

0 投票

The boxplot command creates a group of lines, so if you wanted to adjust the box widths you would need to manually adjust the XData properties on each line to move them to the desired locations.
The better alternative is to use the new boxchart command. The object created by the the boxchart command has a BoxWidth property, but I suspect you won't need it, because even when you specify x values, the boxes created by the boxchart command have uniform widths.
boxchart(randn(1000,1), 'BoxWidth', 0.3);
or
boxchart(randi(10,1000,1),randn(1000,1));

4 件のコメント

Adam Danz
Adam Danz 2021 年 4 月 30 日
編集済み: Adam Danz 2021 年 4 月 30 日
An extension of Benjamin Kraus's example where the width of each box is specified using boxchart.
rng('default')
y = rand(20,5).*randi(4,1,5) + rand(1,5)*2;
x = 1:size(y,2); % x position of each box
widths = [.25 .6 .1 .9, .45]; % width of each box (0:1)
figure();
ax = axes();
% X-axis limits must be set prior to adding the boxes
% and they are apparently categorical even if the
% inputs are numeric. Here's an easy way to set
% the x-limits to the range of your data (deleted later)
temp = plot(ax, categorical(x), nan(1,5));
hold(ax,'on')
xl = xlim(ax); % Store original axis limits, boxchart may change them
for i = 1:size(y,2)
xdata = repelem(x(i),size(y,1),1); % Specify x=coordinate of box
boxchart(ax,y(:,i),'XData',xdata,'BoxWidth',widths(i))
end
xlim(ax,xl) % Restore xlimits
delete(temp)
Benjamin Kraus
Benjamin Kraus 2021 年 4 月 30 日
編集済み: Benjamin Kraus 2021 年 4 月 30 日
@Adam Danz The reason the x-axis is categorical is because you are setting the XData using a name-value pair. If you specify the x-data before the y-data, it will create a numeric ruler.
Do this:
boxchart(ax,xdata,y(:,i),'BoxWidth',widths(i))
Instead of this:
boxchart(ax,y(:,i),'XData',xdata,'BoxWidth',widths(i))
With that tweak, your code now becomes:
rng('default')
y = rand(20,5).*randi(4,1,5) + rand(1,5)*2;
x = 1:size(y,2); % x position of each box
widths = [.25 .6 .1 .9, .45]; % width of each box (0:1)
figure();
ax = axes();
hold(ax,'on')
for i = 1:size(y,2)
xdata = repelem(x(i),size(y,1),1); % Specify x=coordinate of box
boxchart(ax,xdata,y(:,i),'BoxWidth',widths(i))
end
Adam Danz
Adam Danz 2021 年 4 月 30 日
Thanks for the explanation, Benjamin.
I wonder what the logic is in converting numeric inputs to categorical under one syntax but not the other.
Gechang Cao
Gechang Cao 2021 年 5 月 1 日
Thank you very much! Cool, the box widths now are controlled by some numbers. I tried some means but did not make it. The locations of x-data are also controlled by the real values not by even distance. Thanks a lot! This is really cool!!

サインインしてコメントする。

その他の回答 (1 件)

markus tripolt
markus tripolt 2024 年 8 月 18 日

0 投票

Hi all,
I have a additional question. Is there a function to set the box width according to the number of data points automatecally, or is the only way to calc the percentage per hand typ it to the width vector?
Thanks Markus

1 件のコメント

Benjamin Kraus
Benjamin Kraus 2024 年 8 月 19 日
@markus tripolt: If you have a new question, your best bet is to post a new question to MATLAB Answers, rather than adding an "answer" to an existing question.
There is no method to automatically set the box width based on the number of data points. I'm not sure how you would use the number of data points to calculate the box widths.
If you post a new question, can you clarify how you want to convert the "number of data points" into the box width"? In addition, please clarify whether you are referring to boxchart or boxplot, because the answers will be very different between the two.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by