Fixing the absolute bar width on 'bar' plot
12 views (last 30 days)
Show older comments
Ruben van den Heuvel
on 12 Oct 2017
Commented: Daniel Smith
on 27 Jun 2019
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
Accepted Answer
dpb
on 13 Oct 2017
Edited: dpb
on 13 Oct 2017
Well, that's bizarre-looking on the surface will grant...I'm not sure how to fix up the bar widths directly but I can give you a workaround kludge that will cause the bars to look like they do for the second.
Using
bar([x1 x1(end)+0.01],[y1 nan]
will cause the narrow bars which is owing to the difference in minimum spacing between the two x vectors by some internal logic. In the first case min(diff(x1)) --> 1 where as with x2 the minimum bar separation is 0.027 with the particular set of rn's; that will, obviously be different from run to run but is bound to be <<1 for this many values.
Augmenting the integer-valued vector with a value that is only a short dx from an existing value (I just chose last for convenience in placement in the [] expression) causes the need to have closely-spaced bars as does the given x2; using a NaN for the corresponding y2 makes for the required matching vector lengths but is silently ignored so doesn't affect the displayed plot.
If get a chance, you might check if your release still has bar as an m-file and see if could find the subject logic that defines that subsequent width for the objects; again I've got other commitments so can't do that level of snooping at the moment.(*)
Hopefully that will give you a workaround that can live with...if it's the other way you want the data to look, that can't be as then the bars are wider than the x-spacing; you'd have to group via hist. Hence, I presume you'd prefer the first of your original figures to look like the second that is the goal.
(*) I did open the file; I'd forgotten the guts are in some helper functions makebars is the main one. I see that inside it if the x-spacing is unequal that it then sets the width based on the minimum spacing between points so, in fact, it does basically what I thought from the above; your two cases both are unequally-spaced, but the magnitude difference in the minimum difference causes enough difference in the computed width internally to be very noticeable--making the first roughly the same magnitude creates similar-looking result.
3 Comments
dpb
on 16 Oct 2017
Yeah, we're used to thinking the bar width is dependent upon the number of bars instead of the x-spacing.
bar is very difficult function with which to work and the HG2 release has mucked things up even further by limiting access to the internals and converting from HG1 wherein the bars were patch objects that one could (with some effort) retrieve the coordinates of which and mung on them. Now they're a higher-abstracted bar object with a bunch of hidden properties that can't get at directly at all in order to be able to do stuff like reset the patch coordinates; hence the kludge to fix up the dx between the two to be nearly the same.
I've harped on TMW for 20+ year about the mess it is to try to do routine things with barplots with no success whatever in really fixing any of the issues; in fact, they've generally made things worse rather than better... :(
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
0 Comments
See Also
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!