Doug's MATLAB Video Tutorials

June 12th, 2009

Using HIST and BAR to customize your histograms

I recently had a question come in via e-mail where this MATLAB user wanted to be able to change the look of her histogram. The key to this problem was using HIST to generate the binning data and then use the more flexible BAR to make the bar chart look exactly as she wished. If HIST is used without a left hand side argument, then the function will make a plot using some default choices. If you want more control over the graph, you will have to use the flexibility of BAR to do that.

12 Responses to “Using HIST and BAR to customize your histograms”

  1. Marco replied on :

    Hi doug,
    can I ask you a question about the histogram of an image?
    Here the simple code:

    N=200;
    A=randn(N,N);
    imshow(A), figure, imhist(A), axis tight

    The histogram does not look right. I am expecting a Gaussian looking centered at zero…
    thanks a lot,
    Marco

  2. Steve Eddins replied on :

    Marco—imhist assumes a dynamic range of [0,1] when you pass it a floating-point image, so it uses bins in that range.

  3. Shalini replied on :

    Can I have some tips on how to write a MATLAB code to plot a histogram without using the function “hist”

    Thanks,
    Shalini

  4. dhull replied on :

    @Shalini,

    Why would you want to?

    You can use the output from hist as input to BAR.m if you want finer control.

  5. Shalini replied on :

    I’m supposed to write a code for a histogram without using the hist function

  6. dhull replied on :

    @shalini

    >>edit hist

    Take a look at how it was implemented.

  7. Chitra replied on :

    Is it possible to put error bars for a histogram. the error bar will be the standard deviation of the values used to plot the histogram.

    Chitra

  8. dhull replied on :

    Never tried this, but it might work:

    http://www.mathworks.com/matlabcentral/fileexchange/25613-barrerror-utility-for-bar-plots-with-automatic-error-bars

  9. maria replied on :

    Hi Doug,
    i want to ask you a question.
    I want a function which creates a histogram where the x axis will consist words and y axis numbers. For example, i have the names {‘Paul’ ‘Kate’ ‘John’ ‘Alex’} and their ages {23 40 16 32} in a cell array 4×2. I want the names at x axis and ages at y axis.
    Is there any function to do this?

    Thanks a lot..

  10. dhull replied on :

    @maria,

    You can change the ‘yTickLabel’ property of the axes with set.

    Doug

  11. winnie replied on :

    Hi Doug
    i would like to use hist command to help to plot a frequency table with class size of 5, is it possible?
    eg:
    input=1 2 3 7 8 9
    table=
    [ 1-5 3 ;
    6-10 3 ]

    thx a lot =)

  12. dhull replied on :

    @Winnie,
    You will want to choose the bins specifically as shown in the documentation for HIST.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


MathWorks

Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

Doug's picture

These postings are the author's and don't necessarily represent the opinions of The MathWorks.