(Not recommended) Histogram plot - MATLAB hist (2024)

(Not recommended) Histogram plot

collapse all in page

hist is not recommended. Use histogram instead.

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

Syntax

hist(x)

hist(x,nbins)

hist(x,xbins)

hist(ax,___)

counts = hist(___)

[counts,centers]= hist(___)

Description

example

hist(x) creates a histogram bar chart of the elements in vector x. The elements in x are sorted into 10 equally spaced bins along the x-axis between the minimum and maximum values of x. hist displays bins as rectangles, such that the height of each rectangle indicates the number of elements in the bin.

If the input is a multi-column array, hist creates histograms for each column of x and overlays them onto a single plot.

If the input is of data type categorical, each bin is a category of x.

example

hist(x,nbins) sorts x into the number of bins specified by the scalar nbins.

hist(x,xbins) sorts x into bins with intervals or categories determined by the vector xbins.

  • If xbins is a vector of evenly spaced values, then hist uses the values as the bin centers.

  • If xbins is a vector of unevenly spaced values, then hist uses the midpoints between consecutive values as the bin edges.

  • If x is of data type categorical, then xbins must be a categorical vector or cell array of character vectors that specifies categories. hist plots bars only for those categories.

The length of the vector xbins is equal to the number of bins.

hist(ax,___) plots into the axes specified by ax instead of into the current axes (gca). The option ax can precede any of the input argument combinations in the previous syntaxes.

counts = hist(___) returns a row vector, counts, containing the number of elements in each bin.

example

[counts,centers]= hist(___) returns an additional row vector, centers, indicating the location of each bin center on the x-axis.

Examples

collapse all

Histogram of Vector

x = [0 2 9 2 5 8 7 3 1 9 4 3 5 8 10 0 1 2 9 5 10];hist(x)

(Not recommended) Histogram plot - MATLAB hist (1)

hist sorts the values in x among 10 equally spaced bins between the minimum and maximum values in the vector, which are 0 and 10 in this example.

Histogram of Multiple Columns

Open Live Script

Generate three columns of 1,000 random numbers and plot the three column overlaid histogram.

x = randn(1000,3); hist(x)

(Not recommended) Histogram plot - MATLAB hist (2)

The values in x are sorted among 10 equally spaced bins between the minimum and maximum values. hist sorts and bins the columns of x separately and plots each column with a different color.

Specify Number of Histogram Bins

Open Live Script

Plot a histogram of 1,000 random numbers sorted into 50 equally spaced bins.

x = randn(1000,1); nbins = 50;hist(x,nbins)

(Not recommended) Histogram plot - MATLAB hist (3)

Use hist to Calculate Only

Open Live Script

Generate 1,000 random numbers. Count how many numbers are in each of 10 equally spaced bins. Return the bin counts and bin centers.

x = randn(1000,1); [counts,centers] = hist(x)
counts = 1×10 4 27 88 190 270 243 123 38 13 4
centers = 1×10 -2.8915 -2.2105 -1.5294 -0.8484 -0.1673 0.5137 1.1947 1.8758 2.5568 3.2379

Use bar to plot the histogram.

bar(centers,counts)

(Not recommended) Histogram plot - MATLAB hist (4)

Open Live Script

Generate 1,000 random numbers and create a histogram.

data = randn(1000,1);hist(data)

(Not recommended) Histogram plot - MATLAB hist (5)

Get the handle to the patch object that creates the histogram plot.

h = findobj(gca,'Type','patch');

Set the face color of the bars plotted to an RGB triplet value of [0 0.5 0.5]. Set the edge color to white.

h.FaceColor = [0 0.5 0.5];h.EdgeColor = 'w';

(Not recommended) Histogram plot - MATLAB hist (6)

Input Arguments

collapse all

xInput array
vector or matrix

Input vector or matrix.

  • If x is a vector, then hist creates one histogram.

  • If x is a matrix, then hist creates a separate histogram for each column and plots the histograms using different colors.

If the input array contains NaNs or undefined categorical values, hist does not include these values in the bin counts.

If the input array contains the infinite values -Inf or Inf, then hist sorts -Inf into the first bin and Inf into the last bin. If you do not specify the bin intervals, then hist calculates the bin intervals using only the finite values in the input array.

Data Types: single|double|logical|categorical

nbinsNumber of bins
10 (default) | scalar

Number of bins. Input x must be numeric, not categorical.

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

xbinsBin locations or categories
vector

Bin locations or categories, specified as a vector.

If x is numeric or logical, then xbins must be of type single or double.

  • If the elements in xbins are equally spaced, then these elements are the bin centers.

  • If the elements in xbins are not equally spaced, then these elements are indicated by markers along the x-axis, but are not the actual bin centers. Instead, hist calculates the bin edges as the midpoints between consecutive elements in vector xbins. To specify the bin edges directly, use histc.

  • xbins must contain only finite values. The first and last bins extend to cover the minimum and maximum values in x.

If x is categorical, then xbins must be a categorical vector or cell array of character vectors that specifies categories. hist plots bars only for those categories specified by xbins.

axAxes object
axes object

Axes object. Use ax to plot the histogram in a specific axes instead of the current axes (gca).

Output Arguments

collapse all

counts — Counts of the number of elements in each bin
row vector

Counts of the number of elements in each bin, returned as a row vector.

centers — Bin centers or categories
vector

Bin centers or categories, returned as a vector. If used with the syntax [counts,centers] = hist(x,xbins), then the centers output has the same elements as the xbins input.

  • If x is numeric or logical, then centers is a numeric row vector.

  • If x is categorical, then centers is a cell array of character vectors.

Extended Capabilities

Version History

Introduced before R2006a

See Also

bar | histc | mode | patch | rose | stairs | 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 plot - MATLAB hist (7)

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 plot - MATLAB hist (2024)

FAQs

How to plot histogram in matlab without using hist function? ›

Direct link to this question
  1. Pie=sin(2*pi*tt); %array of data p=sin(2*pi*t/T)
  2. L=10; %number of bins.
  3. minPie = min(Pie); %min value of array.
  4. maxPie = max(Pie); %max value of array.
  5. binwidth = (maxPie - minPie)/L; %width of bin.
  6. binnum = 1+floor((Pie - minPie) / binwidth); %value of array to a bin.
Jan 28, 2022

What is the difference between hist and histogram in Matlab? ›

The hist function includes values falling on the right edge of each bin (the first bin includes both edges), whereas histogram includes values that fall on the left edge of each bin (and the last bin includes both edges). Shift the bin edges slightly to obtain the same bin counts as hist .

What is the best alternative to a histogram? ›

A density curve, or kernel density estimate (KDE), is an alternative to the histogram that gives each data point a continuous contribution to the distribution. In a histogram, you might think of each data point as pouring liquid from its value into a series of cylinders below (the bins).

How do you plot an error histogram? ›

Plot the Error Histogram

Compute the error values as the difference between target values and predicted values. error = targets - outputs; number_of_bins = 10; ploterrhist(error,'bins',number_of_bins); The plot shows an error histogram with 10 bins.

When would you use a histogram instead of a dot plot? ›

Data can be represented in various ways such as dot plots, histograms, and box plots. Dot plots and box plots are useful for finding the median, while histograms are great for showing the number of values within a specific range.

How do you manually make a histogram? ›

We can use the following steps to construct a histogram:
  1. Step 1: On the graph, draw two perpendicular lines- horizontal axis and vertical axis and label them.
  2. Step 2: Give the graph a title.
  3. Step 3: Label the horizontal axis. ...
  4. Step 4: Label the vertical axis.
Mar 24, 2024

What is the limit of histogram in Matlab? ›

If you specify BinMethod for datetime or duration data, then histogram can use a maximum of 65,536 bins (or 216). If the specified bin duration requires more bins, then histogram uses a larger bin width corresponding to the maximum number of bins.

Why do we use histogram in Matlab? ›

Description. Histograms are a type of bar plot that group data into bins. After you create a Histogram object, you can modify aspects of the histogram by changing its property values. This is particularly useful for quickly modifying the properties of the bins or changing the display.

What is the difference between hist and Barplot? ›

Histograms and bar charts display different types of data

Histograms visualize quantitative data or numerical data, whereas bar charts display categorical variables. In most instances, the numerical data in a histogram will be continuous (having infinite values).

What is better than a histogram? ›

You should choose a bar chart when you want to compare different categories or types of data. But if you want to understand the distribution and frequency of a single set of data, go with a histogram.

What is not an appropriate use of a histogram? ›

Answer: A histogram is not appropriate for displaying cumulative frequency.

What makes a histogram incorrect? ›

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.

What is histogram problem? ›

A histogram is a graphical representation of a grouped frequency distribution with continuous classes. It is an area diagram and can be defined as a set of rectangles with bases along with the intervals between class boundaries and with areas proportional to frequencies in the corresponding classes.

What are some possible causes of histogram analysis errors? ›

If the radiographer selects a part other than the one imaged, a histogram analysis error may occur. In addition, any errors that occur, such as during data extraction from the IR or rescaling during computer processing, could affect the exposure indicator and provide a false value.

How do you plot without a legend in Matlab? ›

1- Select the curve you don't want have legend. 2- Go to the "more properties" (while the curve is still selected). 3- Turn "HandleVisibility" off.

Can you make a histogram without bins? ›

If you create a histogram without specifying the bins (i.e., you leave the Bin Range empty), it would still create the histogram. It would automatically create six equally spaced bins and used this data to create the histogram.

How do you plot a simple histogram? ›

How to Plot Histogram?
  1. Begin by marking the class intervals on the X-axis and frequencies on the Y-axis.
  2. The scales for both the axes have to be the same.
  3. Class intervals need to be exclusive.
  4. Draw rectangles with bases as class intervals and corresponding frequencies as heights.

Top Articles
Latest Posts
Article information

Author: Errol Quitzon

Last Updated:

Views: 5757

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.