Effacer les filtres
Effacer les filtres

Adjusting the spacing between the bars of different groups within a category using boxchart.

38 vues (au cours des 30 derniers jours)
I am using boxchart to plot data as in the example: openExample('graphics/UseCombinationOfGroupingVariablesExample')
How do I reduce the spacing between the groups (blue and red) so that they touch each other for every category? This is required for me as I have six groups. The spacing between the goups makes it difficult to differentiate between boxes of different categories. I want the boxes of each category to be clubbed together. Any suggestions would be highly appreciated.

Réponses (1)

Austin M. Weber
Austin M. Weber le 22 Mai 2024
If you set the BoxWidth name-value pair to 1 then the boxcharts will all touch each other:
load temperature_data % A random data table that I have attached similar to the one in the prompt
figure(1)
b = boxchart(data.Month, data.TemperatureF,...
'GroupByColor',data.Year,...
'MarkerStyle','.',...
'BoxWidth',1); % <-- This line makes the boxcharts touch
legend(categorical(unique(data.Year)),'Location','eastoutside')
Unfortunately, this doesn't add space between the monthly categories. There really isn't an easy way to do that as far as I can tell. I belive you would have to adjust the positions of the individual patch objects in order to accomplish that, which would be very tedious.
My recommendation is to use the groupedBoxchart function from the Matlab Central File Exchange (click here). This function is really simple to use, and it does an excellent job at using colored bands to help distinguish the groupings for each category. For example:
b2 = groupedBoxchart(data.Month, data.Year, data.TemperatureF, "MarkerStyle", '.');
ylabel('Temperature (\circF)')
ylim([20 100])
L = legend(categorical(unique(data.Year)),'Location','north');
L.NumColumns = 3;
  2 commentaires
Sudhee
Sudhee le 23 Mai 2024
Thank you for your suggestion @Austin M. Weber. Its a limitation of boxchart then.
Austin M. Weber
Austin M. Weber le 23 Mai 2024
Modifié(e) : Austin M. Weber le 23 Mai 2024
@Sudhee, I have thought of another solution but it is a little ugly. It involves adding synthetic "empty" categories to your table in order to add spaces between the x-axis categories. Here is one way to do it:
load temperature_data
% Adjust the category order so that the "empty" categories fit in-between
% the original categories.
new_category_order = {'Jun','Empty1','Jul','Empty2','Aug','Empty3','Sep','Empty4','Oct'};
data.Month = categorical(data.Month,new_category_order);
% Create your boxchart figure and set BowWidth=1 so that the boxes are
% "clubbed together", as you say.
figure()
boxchart(data.Month,data.TemperatureF,'GroupByColor',data.Year,'BoxWidth',1)
% Set the "empty" categories to invisible characters so that they do not
% show up as x-axis labels
xt = xticks;
xt([2 4 6 8]) = categorical(" "); % Invisible character (ATL+255), not a space!
xticklabels(xt)
% Optional plot details
ylabel('Temperature (\circF)'), ylim([20 100])
L=legend(categorical(unique(data.Year)),'Location','northeast');
L.NumColumns = 3;
set(gca,'TickDir','none') % disables the additional x-tick marks from showing (at the expense of also removes the y-axis ticks...)
set(gca,'YGrid','on') % add horizontal gridlines to replace the missing y-tick marks

Connectez-vous pour commenter.

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by