Fixing the absolute bar width on 'bar' plot

12 Ansichten (letzte 30 Tage)
Ruben van den Heuvel
Ruben van den Heuvel am 12 Okt. 2017
Kommentiert: Daniel Smith am 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 Kommentare
Ruben van den Heuvel
Ruben van den Heuvel am 12 Okt. 2017
Thanks, I will try to upload some sample set tomorrow
Ruben van den Heuvel
Ruben van den Heuvel am 13 Okt. 2017
This code sort of mimics my data set. The first x-set look like integers (but are floats) and the second x-set has decimal numbers (also floats):
x1 = sort(randperm(100,50))
y1 = 1E-11*rand(1,numel(x1))
x2 = sort(100*rand(50,1))
y2 = 1E-11*rand(1,numel(x2))
bar(x1,y1)
axis([0 100 1E-13 1E-10])
set(gca,'YScale','log')
figure
bar(x2,y2)
axis([0 100 1E-13 1E-10])
set(gca,'YScale','log')

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 13 Okt. 2017
Bearbeitet: dpb am 13 Okt. 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 Kommentare
dpb
dpb am 16 Okt. 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... :(
Daniel Smith
Daniel Smith am 27 Jun. 2019
This is SO helpful. Thank you.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Zelda Grey
Zelda Grey am 26 Dez. 2018
Bearbeitet: Zelda Grey am 26 Dez. 2018
I find this applicable. If you are over plotting many histogram
  1. Choose the one of the data as your baseline for your calculation.
  2. Find the difference between two consecutive rows of that data set. (A data set)
  3. Find the difference between two consecutive rows of other data set. (B data set)
  4. 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

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by