(Not recommended) Histogram bin counts (2024)

(Not recommended) Histogram bin counts

collapse all in page

histc is not recommended. Use histcounts instead.

For more information, including suggestions on updating code, see Replace Discouraged Instances of hist and histc.

Syntax

bincounts = histc(x,binranges)

bincounts = histc(x,binranges,dim)

[bincounts,ind] =histc(___)

Description

example

bincounts = histc(x,binranges) counts the number of values in x that are within each specified bin range. The input, binranges, determines the endpoints for each bin. The output, bincounts, contains the number of elements from x in each bin.

  • If x is a vector, then histc returns bincounts as a vector of histogram bin counts.

  • If x is a matrix, then histc operates along each column of x and returns bincounts as a matrix of histogram bin counts for each column.

To plot the histogram, use bar(binranges,bincounts,'histc').

bincounts = histc(x,binranges,dim) operates along the dimension dim.

example

[bincounts,ind] =histc(___) returns ind, an array the same size as x indicating the bin number that each entry in x sorts into. Use this syntax with any of the previous input argument combinations.

Examples

collapse all

Create Histogram Plot

Open Live Script

Initialize the random number generator to make the output of randn repeatable.

rng(0,'twister')

Define x as 100 normally distributed random numbers. Define bin ranges between -4 and 4. Determine the number of values in x that are within each specified bin range. Return the number of elements in each bin in bincounts.

x = randn(100,1);binranges = -4:4;[bincounts] = histc(x,binranges)
bincounts = 9×1 0 2 17 28 32 16 3 2 0

To plot the histogram, use the bar function.

figurebar(binranges,bincounts,'histc')

(Not recommended) Histogram bin counts (1)

Return Bin Numbers for Histogram

Open Live Script

Defined ages as a vector of ages. Sort ages into bins with varying ranges between 0 and 75.

ages = [3,12,24,15,5,74,23,54,31,23,64,75];binranges = [0,10,25,50,75];[bincounts,ind] = histc(ages,binranges)
bincounts = 1×5 2 5 1 3 1
ind = 1×12 1 2 2 2 1 4 2 4 3 2 4 5

bincounts contains the number of values in each bin. ind indicates the bin numbers.

Input Arguments

collapse all

xValues to be sorted
vector | matrix

Values to be sorted, specified as a vector or a matrix. The bin counts do not include values in x that are NaN or that lie outside the specified bin ranges. If x contains complex values, then histc ignores the imaginary parts and uses only the real parts.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32

binrangesBin ranges
vector | matrix

Bin ranges, specified as a vector of monotonically nondecreasing values or a matrix of monotonically nondecreasing values running down each successive column. The values in binranges determine the left and right endpoints for each bin. If binranges contains complex values, then histc ignores the imaginary parts and uses only the real parts.

If binranges is a matrix, then histc determines the bin ranges by using values running down successive columns. Each bin includes the left endpoint, but does not include the right endpoint. The last bin consists of the scalar value equal to last value in binranges.

For example, if binranges equals the vector [0,5,10,13], then histc creates four bins. The first bin includes values greater than or equal to 0 and strictly less than 5. The second bin includes values greater than or equal to 5 and less than 10, and so on. The last bin contains the scalar value 13.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32

dimDimension along which to operate
scalar

Dimension along which to operate, specified as a scalar.

Output Arguments

collapse all

bincounts — Number of elements in each bin
vector | matrix

Number of elements in each bin, returned as a vector or a matrix. The last entry in bincounts is the number of values in x that equal the last entry in binranges.

ind — Bin index numbers
vector | matrix

Bin index numbers, returned as a vector or a matrix that is the same size as x.

Tips

  • If values in x lie outside the specified bin ranges, then histc does not include these values in the bin counts. Start and end the binranges vector with -inf and inf to ensure that all values in x are included in the bin counts.

Extended Capabilities

Version History

Introduced before R2006a

See Also

bar | hist | mode | histogram | histcounts

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

(Not recommended) Histogram bin counts (2)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

Contact your local office

(Not recommended) Histogram bin counts (2024)

FAQs

What is the recommended number of bins for a histogram? ›

Choose between 5 and 20 bins. The larger the data set, the more likely you'll want a large number of bins. For example, a set of 12 data pieces might warrant 5 bins but a set of 1000 numbers will probably be more useful with 20 bins. The exact number of bins is usually a judgment call.

What is the rule of thumb for histogram bins? ›

Several rules of thumb exist for determining the number of bins, such as the belief that between 5 and 20 bins is usually adequate (for example, Matlab uses 10 bins as a default).

Why is too many bins in a histogram bad? ›

If you have too many bins, then the data distribution will look rough, and it will be difficult to discern the signal from the noise. On the other hand, with too few bins, the histogram will lack the details needed to discern any useful pattern from the data.

What is Scott's rule for number of bins? ›

The default 'auto' algorithm chooses a bin width to cover the data range and reveal the shape of the underlying distribution. Scott's rule is optimal if the data is close to being normally distributed. This rule is appropriate for most other distributions, as well. It uses a bin width of 3.5*std(X(:))*numel(X)^(-1/3) .

Why is the bin size important in a histogram? ›

A 'bin' for each sample point gives us no more information, but only stretches the width of the chart. A good bin width will usually show a recognizable normal probability distribution curve, unless the data is really multi-modal. Then there could be two or more distinct 'humps' in the histogram chart.

What is optimal binning for histogram? ›

optBINS (optimal binning) determines the optimal number of bins in a uniform bin-width histogram by deriving the posterior probability for the number of bins in a piecewise-constant density model after assigning a multinomial likelihood and a non-informative prior.

Do histogram bins have to be equal? ›

Thus varying the bin width within a histogram can be beneficial. Nonetheless, equal-width bins are widely used. Several rules of thumb exist for determining the number of bins, such as the belief that between 5 and 20 bins is usually adequate.

How do I choose a bin range for a histogram in Excel? ›

Part 2: How to Adjust the Histogram Bin Range in Excel?
  1. Step 1: Open Excel and Prepare Your Data.
  2. Step 2: Go to Data Analysis ToolPak.
  3. Step 3: Choose Histogram and Click "OK"
  4. Step 4: Select the Input Range and Bin Range.
  5. Step 5: Specify Output Options.
  6. Step 6: Click "OK" to Generate the Histogram.
Jul 27, 2023

What is the maximum number of bins in a histogram? ›

There is no hard maximum for the number of bins in a histogram. If the variable being plotted is continuous, then an argument can be made for an infinite number of categories (and the histogram basically becomes a rug plot). The number of points in the data set is not an appropriate upper bound.

What makes a bad histogram? ›

A histogram can be misleading if it has a deceptive scale and/or inappropriate starting and ending points on the y-axis. Watch the scale on the y-axis of a histogram. If it goes by large increments and has an ending point that's much higher than needed, you see a great deal of white space above the histogram.

Why would a histogram not be an appropriate? ›

A histogram would not be appropriate because histograms are used for continuous data and the data obtained is not continuous.

How to calculate optimal number of bins? ›

The Freedman-Diaconis rule is very robust and works well in practice. The bin-width is set to h=2×IQR×n−1/3. So the number of bins is (max−min)/h, where n is the number of observations, max is the maximum value and min is the minimum value.

What is the rice rule for histograms? ›

We prefer the Rice rule, which is to set the number of intervals to twice the cube root of the number of observations. In the case of 1000 observations, the Rice rule yields 20 intervals instead of the 11 recommended by Sturges' rule.

What is the Sturges rule for bins? ›

Sturges' Rule Formula

The following formula is used to calculate the optimal number of bins in a histogram using sturges' rule. To calculate the optimal number of bins using sturges' rule, take the log base 2 of the number of observations in the data set, then add 1 to this result.

How many buckets should a histogram have? ›

There is no strict rule on how many bins to use—we just avoid using too few or too many bins.

How do you find the optimal number of bins? ›

The Freedman-Diaconis rule is very robust and works well in practice. The bin-width is set to h=2×IQR×n−1/3. So the number of bins is (max−min)/h, where n is the number of observations, max is the maximum value and min is the minimum value.

What is the recommended number of classes for a histogram? ›

Typically, no fewer than 5 and no more than 20 class intervals work best for a frequency histogram. Choose the first class interval to include your lowest (smallest value) data and make sure that no overlap exists so that one piece of data does not fall into two class intervals.

Top Articles
Latest Posts
Article information

Author: Cheryll Lueilwitz

Last Updated:

Views: 5747

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Cheryll Lueilwitz

Birthday: 1997-12-23

Address: 4653 O'Kon Hill, Lake Juanstad, AR 65469

Phone: +494124489301

Job: Marketing Representative

Hobby: Reading, Ice skating, Foraging, BASE jumping, Hiking, Skateboarding, Kayaking

Introduction: My name is Cheryll Lueilwitz, I am a sparkling, clean, super, lucky, joyous, outstanding, lucky person who loves writing and wants to share my knowledge and understanding with you.