How to change distance between groups of bars in a bar plot?

50 views (last 30 days)
I have 3 sets of data that I am plotting using 'bar'. This generates sets of grouped bars. I know one can change the width of bars such that within a group they have some gap between them.
But how do I change the distance between groups of bars? I could not find any solution online, except calculating the x coordinates by yourself and plotting each dataset separately. If that's the only way, Mathworks should do something about it!
  2 Comments
dpb
dpb on 5 Dec 2014
Not sure what's wrong with that...what would you want some automagic input parameter to specify except the same thing that you would still have to calculate?
There's no need to plot datasets separately, simply use the x-spacing you calculate in the call to bar You may have to rearrange ticks and tick labels if the spacing doesn't reflect the desired labels.
Without an example of what you're trying to create can't say more than generalities...attach a sample???
dpb
dpb on 18 Dec 2021
OK, I just ran into the issue -- with a fairly sizable number of bars/group, there's a lot of wasted white space between the bar groups that could be utilized to make the bars within the groups wider. Here's the one that getting here...even if you stretch the axis height by making figure larger, the corresponding whitespace just grows proportionally..
To get these as wide as they are took closing up ylim, setting 'Width',1 and reducing fontsizes for title and labels and putting the subtitle as the legend title and putting it 'eastoutside' instead of four-column wide at bottom. Still there's insufficient bar witdth for the values to fit while there's still not enough room for values unless make fontsize even smaller. Meanwhile, there's all that white real estate in between the groups...

Sign in to comment.

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 26 Apr 2024
Starting in R2024a, you can now customize the width of each group of bars using the new GroupWidth property.
For example:
nexttile
bar(magic(5), GroupWidth=0.4);
nexttile
bar(magic(5), GroupWidth=1);

More Answers (1)

Hunter Pruett
Hunter Pruett on 23 May 2020
Just finished the barmod function--controls relative position of just about everything in the plot. Hope this helps anyone else with this question.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!