Fixing the absolute bar width on 'bar' plot
Show older comments
When using the 'bar' function, is it somehow possible to fix the absolute bar width? I have multiple bar figures that at the moment have different bar widths (even with the same x-axis), but I would like to have the bars look the same.
(Matlab R2015b)
4 Comments
dpb
on 12 Oct 2017
AFAIK, not directly, no. The width parameter as you've undoubtedly learned is a relative spacing between bars and the actual width is computed internally dependent upon the style and number of bars.
I'm (almost) certain there'd be a way to dig into the internals and manipulate the properties to produce the effect but don't know a magic formulation otomh.
I'd suggest would help for specific case to post a sample dataset you'd like to manipulate so folks don't spend time on a different style or such that might not be applicable to your actual case.
dpb
on 12 Oct 2017
" bar figures that at the moment have different bar widths (even with the same x-axis)"
Well, actually, if I try
>> hF1=figure;
>> bar(rand(5,1))
>> xL1=xlim;
>> hF2=figure;
>> bar(rand(4,1))
>> xlim(xL1)
>>
then the two do have the same barwidths such that if overlay the two the bars are identially-sized in width. Now, of course, there's a bunch of blank/white space at the RH end of the axis for the second figure, but the widths are the same.
Now, to have the axes fit the data and still then have the same width bar would have to dig more than have time for at the moment...maybe this evening.
Ruben van den Heuvel
on 12 Oct 2017
Ruben van den Heuvel
on 13 Oct 2017
Accepted Answer
More Answers (1)
Zelda Grey
on 26 Dec 2018
Edited: Zelda Grey
on 26 Dec 2018
I find this applicable. If you are over plotting many histogram
- Choose the one of the data as your baseline for your calculation.
- Find the difference between two consecutive rows of that data set. (A data set)
- Find the difference between two consecutive rows of other data set. (B data set)
- Divide smallest dofference to biggest one and plot it
A=randi(10,5,2);
B=randi(10,5,2);
Adiff=abs(A(2,1)-A(1,1))% Make sure to get absolute value not negative
Bdiff=abs(B(2,1)-B(1,1))
BWitdh=Adiff/Bdiff%% The difference proportion to using bar width
figure('Name','Bar Width Same size')
bar(A(:,1),A(:,2),'FaceColor',[0 0 1],'FaceAlpha',0.8,'BarWidth',BWitdh)
hold on
bar(B(:,1),B(:,2),'FaceColor',[1 0 0],'FaceAlpha',0.4,'BarWidth',BWitdh)

Unfortunately there is a gap between the bars but at least you can get them at the same size
Categories
Find more on Annotations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!