Is it possible to extract bars from the hist or histogram function?

1 view (last 30 days)
I would like to extract the bar-rectangle coordinates (the shapes themself) from the histogram plot. Is it possible?
  8 Comments
Mr M.
Mr M. on 30 Sep 2015
Edited: Walter Roberson on 30 Sep 2015
sorry, I am totally stupid, I do not understand the code.
I tried something like this, but no effect:
data = [rand(1,50)];
xlim([0 2]);
ylim([0 100]);
h1 = hgtransform('Matrix',makehgtform('scale',0.5));
[~,h2] = hist(data,'Parent',h1);
drawnow;
Walter Roberson
Walter Roberson on 30 Sep 2015
data = [rand(1,50)];
xlim([0 2]);
ylim([0 100]);
h1 = hgtransform('Matrix',makehgtform('scale',0.5));
[n,x] = hist(data);
bar(x, n, 'Parent', h1);
drawnow;
The difficulty that you ran into is that when you specify output variables, hist() does not draw the histogram, and instead just returns the computed data. The newer histogram() function always draws the data and returns the handle.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 28 Sep 2015
If you are using histogram() to do the plot, then you can access the histogram properties
If you are using hist() to do the plot, then it uses bar() to do the plots, and bar() creates a patch() object; see http://www.mathworks.com/help/matlab/ref/hist.html#btsgtr1-1_1 . You can examine the properties of the patch() object. It will not be the most fun you've had all day, but it can be done (I have done it.)

Thorsten
Thorsten on 28 Sep 2015
The values you are looking for are returned by the histogram function:
h = hist(x);
plot(h)
  4 Comments
Mr M.
Mr M. on 30 Sep 2015
but 0.5 and 1.5 is also variables, I mean, determined by the histogram.
Thorsten
Thorsten on 1 Oct 2015
Edited: Thorsten on 1 Oct 2015
0.5 and 1.5 are determined by me, because I decided to have my first bar centered at 1 with a width of 1. You have do choose the x-location of bars and the width of the bars, and use the values provided in h for the height.

Sign in to comment.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!