MATLAB Answers

Width of bar in bar plotting is very high

38 views (last 30 days)
Abdurrehman on 24 Feb 2017
Edited: dpb on 26 Feb 2017
I am using following piece of code in my GUI to plot a bar in an axes, it works very fine when I run GUI in Matlab but when I run this after making its Installer (using Matlab Compiler) the width of bar appears very high. It covers almost 85% of the axes
color=[0 0.25 .5];
hold on;
h.Color=[ 0.945 0.969 0.949];
Please let me know how to resolve this issue.


Sign in to comment.

Answers (1)

dpb on 24 Feb 2017
I'd guess using the 'BarWidth' property will help--it's 0.8 by default, decrease that value for narrower bar widths.


Show 1 older comment
dpb on 25 Feb 2017
If you're really just drawing one bar, width is based on the percentage of the whole axis; in normal use for bar there are multiple bars and so the percentage is of the fraction 1/(N+1) of the axis width for each of the N bars (the axes xlim values are +/-0.5 past the 1:N values by default).
I can't imagine using 1E-14 as width, I'd think that would end up with nothing---oh, I see, anything <1E-5 or so appears as just a single pixel line. If that's the effect wanted, then just use line instead of bar.
But you're really trying to just draw a single bar of a specific dimension, probably using patch directly with a set of specific coordinates is the better choice.
Not sure what the end result desired is...
Muhammad Abdurrehman
Muhammad Abdurrehman on 25 Feb 2017
Actually in my application user can compare/plot 1 to maximum 4 different systems testing results. So there are 4 different bar plots on said axes when 4 system names are entered. The bar width is excellent if user intends to compare/plot 2 to 4 systems. but if only one system name is entered. So in this case the width of bar is expended over 90% of the axes.
dpb on 25 Feb 2017
As said, for the latter case, either:
  1. Heuristically uncover the width value that creates the size bar desired and use the resulting magic number, or
  2. Compute the size/locations and make a patch object directly
seems to me the basic workarounds available.
Oh, I guess a third would be to delve into the guts of bar.m and see if can divine the algorithm used and either patch it or make a copy by another name and fix it to work as desired.
Note that if use width,1 that the bar indeed covers the entire axis and that xlim returns [0.5 1.5]. From this, one could compute the value that covers whatever range of the x-axes you desire the bar to span to compute the width factor.
Unfortunately, with HG2, TMW has made a difficult-to-work-with object (the bar plot) even more so--in fact some things have become essentially impossible. Before the new bar series object, one could retrieve the coordinates of the underlying patch objects from which could compute things like midpoints for bar labels, etc. Here that would let you determine the actual physical x location from the result rather than trying to compute it. It seems that
0.8=dx/1 --> dx=0.8 so 1-0.8/2=0.6
will be the LH bar location by default. If you can define the position you think is the right x-position to get the aspect ratio you like, then you can use the above to compute the width parameter that will create that bar width.

Sign in to comment.

Sign in to answer this question.