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

I would like to extract the bar-rectangle coordinates (the shapes themself) from the histogram plot. Is it possible?

8 Comments

I would like to plot the rectangles shifted and resized in elsewhere, because subplot cannot solve my problem, so I have to make a script to put histograms in a graphic 'matrix'
Is it possible that using a hgtransform() would work for your purpose? If you have a graphics object that does not allow you to directly position it in an axes, you can parent the graphics object to an hgtransform and use that to scale and translate it.
Can you show a picture of the type of "graphic matrix" you want? I'm not quite sure I understand what you're trying to do quite yet.
maybe hgtransform() is the solution, but I cannot figure out how to use. How to associate to the histogram plot, and how to view/plot it after transformation?
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;
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)

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.)
The values you are looking for are returned by the histogram function:
h = hist(x);
plot(h)

4 Comments

But plot(h) makes a lot of other things. I would like to get the rectangles somehow, and use them with the fill() function.
You can use bar(h) to plot rectangles. And you can make your own rectangles from the values return in h. For example, to draw the first bar centered around 1 with a width of 1:
fill([0.5 1.5 1.5 0.5], [ 0 0 h(1) h(1)], 'r')
but 0.5 and 1.5 is also variables, I mean, determined by the histogram.
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

Asked:

on 28 Sep 2015

Edited:

on 1 Oct 2015

Community Treasure Hunt

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

Start Hunting!