Fixing the absolute bar width on 'bar' plot
Afficher commentaires plus anciens
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 commentaires
dpb
le 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
le 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
le 12 Oct 2017
Ruben van den Heuvel
le 13 Oct 2017
Réponse acceptée
Plus de réponses (1)
Zelda Grey
le 26 Déc 2018
Modifié(e) : Zelda Grey
le 26 Déc 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
Catégories
En savoir plus sur Annotations dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!