side by side boxplots

5 ビュー (過去 30 日間)
sai ram gubba
sai ram gubba 2015 年 12 月 22 日
編集済み: Hassaan 2023 年 12 月 25 日
I want to plot a box plot over another box plot but it should look like a grouped bar graph. If we just hold the axes and plot another box plot the 2nd one is on top and the 1st plot is not visible. Please tell me if there is a way to do it.

回答 (1 件)

Hassaan
Hassaan 2023 年 12 月 25 日
編集済み: Hassaan 2023 年 12 月 25 日
Side-by-side boxplots can be created by manipulating the positions of the boxplots manually or by using the grouping variable functionality provided by the boxplot function.
% Sample data
data1 = randn(100, 1); % Data for the first boxplot
data2 = randn(100, 1); % Data for the second boxplot
% Grouping variable
group = [ones(size(data1)); 2 * ones(size(data2))];
% Combine the data into one vector
combinedData = [data1; data2];
% Create the boxplot
boxplot(combinedData, group, 'Labels', {'First', 'Second'});
% Adjust the positions to make them side by side
ax = gca;
lines = ax.Children.Children; % Get the line objects
boxes = lines(arrayfun(@(l) strcmp(l.Type, 'line') && strcmp(l.Tag, 'Box'), lines)); % Find box lines
% Adjust the x-data of the boxes
for i = 1:numel(boxes)
boxes(i).XData = boxes(i).XData + (-1)^(i-1) * 0.15;
end
This code will produce a figure where the two boxplots are displayed side-by-side. Adjusting the XData of the boxes shifts them left and right, respectively, allowing them to be displayed like a grouped bar graph.
The Labels property is used to provide labels for the different groups of boxplots. The adjustment of 0.15 is arbitrary and can be changed depending on how close or far apart you want the boxplots to be.
Please note that this is a simplified example, and in practice, you may need to adjust the positions of other elements (such as whiskers, outliers, and medians) in a similar manner to keep the visual integrity of the boxplot.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by