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

조회 수: 31 (최근 30일)
Ovidiu
Ovidiu 2014년 12월 5일
답변: Benjamin Kraus 2024년 4월 26일 20:06
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
dpb
dpb 2014년 12월 5일
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 2021년 12월 18일
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...

댓글을 달려면 로그인하십시오.

답변 (2개)

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

Benjamin Kraus
Benjamin Kraus 2024년 4월 26일 20:06
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);

카테고리

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