Matplotlib Histogram
We can use the hist() method in pyplot to draw histograms.
The hist() method is a function in the pyplot submodule of the Matplotlib library used to draw histograms.
The hist() method can be used to visualize the distribution of data, such as observing the central tendency, skewness, and outliers.
The syntax of the hist() method is as follows:
matplotlib.pyplot.hist(x, bins=None, range=None, density=False, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, **kwargs)
Parameter Description:
x: Represents the data for which to draw the histogram; can be a one-dimensional array or list.bins: Optional parameter indicating the number of bins in the histogram. Defaults to 10.range: Optional parameter indicating the value range of the histogram; can be a tuple or list. Defaults to None, meaning the minimum and maximum values in the data are used.density: Optional parameter indicating whether to normalize the histogram. Defaults to False, meaning the height of each bin is the number of samples in that bin rather than the frequency or probability density.weights: Optional parameter indicating the weight of each data point. Defaults to None.cumulative: Optional parameter indicating whether to draw a cumulative distribution plot. Defaults to False.bottom: Optional parameter indicating the starting height of the histogram. Defaults to None.histtype: Optional parameter indicating the type of histogram; can be 'bar', 'barstacked', 'step', 'stepfilled', etc. Defaults to 'bar'.align: Optional parameter indicating the alignment of the histogram bins; can be 'left', 'mid', 'right'. Defaults to 'mid'.orientation: Optional parameter indicating the orientation of the histogram; can be 'vertical' or 'horizontal'. Defaults to 'vertical'.rwidth: Optional parameter indicating the width of each bin. Defaults to None.log: Optional parameter indicating whether to use a logarithmic scale on the y-axis. Defaults to False.color: Optional parameter indicating the color of the histogram.label: Optional parameter indicating the label of the histogram.stacked: Optional parameter indicating whether to stack different histograms. Defaults to False.**kwargs: Optional parameter indicating other plotting parameters.
In the following example we simply use hist() to create a histogram:
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
Generate a set of random data
data
=
np.
random
.
randn
(
1000
)
Draw a histogram
plt.
hist
(
data
,
bins
=
30
,
color
=
'skyblue'
,
alpha
=
0.8
)
Set chart properties
plt.
title
(
'RUNOOB hist() Test'
)
plt.
xlabel
(
'Value'
)
plt.
ylabel
(
'Frequency'
)
Display the chart
plt.
show
(
)
The display result is as follows:

The following example demonstrates how to use the hist() function to draw histograms of multiple data groups and compare them:
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
Generate three sets of random data
data1
=
np.
random
.
normal
(
0
,
1
,
1000
)
data2
=
np.
random
.
normal
(
2
,
1
,
1000
)
data3
=
np.
random
.
normal
(
2
,
1
,
1000
)
Draw a histogram
plt.
hist
(
data1
,
bins
=
30
,
alpha
=
0.5
,
label
=
'Data 1'
)
plt.
hist
(
data2
,
bins
=
30
,
alpha
=
0.5
,
label
=
'Data 2'
)
plt.
hist
(
data3
,
bins
=
30
,
alpha
=
0.5
,
label
=
'Data 3'
)
Set chart properties
plt.
title
(
'RUNOOB hist() TEST'
)
plt.
xlabel
(
'Value'
)
plt.
ylabel
(
'Frequency'
)
plt.
legend
(
)
Display the chart
plt.
show
(
)
In the above example we generated three different sets of random data and used the hist() function to draw their histograms. By setting different means and standard deviations, we can generate random data with different distribution characteristics.
We set the bins parameter to 30, which means dividing the data range into 30 equal-width intervals and then counting the frequency of data in each interval.
We set the alpha parameter to 0.5, which means the color transparency of each histogram is 50%.
We used the label parameter to set a label for each histogram so that it is displayed in the legend.
Then we used the legend() function to display the legend. Finally, we used the title(), xlabel(), and ylabel() functions to set the chart title and axis labels.
The display result is as follows:

From the chart above we can clearly see the distribution of these three data sets: data1 and data2 are close to a normal distribution, while data3 is skewed.
This way of comparing histograms can help us analyze and compare the distribution of different data groups.
Combined with Pandas
In the following example we combine Pandas to draw a histogram:
Example
import
numpy
as
np
import
pandas
as
pd
import
matplotlib.
pyplot
as
plt
Generate random numbers using NumPy
random_data
=
np.
random
.
normal
(
170
,
10
,
250
)
Convert the data to a Pandas DataFrame
dataframe
=
pd.
DataFrame
(
random_data
)
Draw a histogram using the Pandas hist() method
dataframe.
hist
(
)
Set chart properties
plt.
title
(
'RUNOOB hist() Test'
)
plt.
xlabel
(
'X-Value'
)
plt.
ylabel
(
'Y-Value'
)
Display the chart
plt.
show
(
)
The display result is as follows:

In addition to a DataFrame, you can also use a Series object in Pandas to draw a histogram. Simply replace the column in the DataFrame with a Series object.
Example
import
numpy
as
np
import
pandas
as
pd
import
matplotlib.
pyplot
as
plt
Generate random data
data
=
pd.
Series
(
np.
random
.
normal
(
size
=
100
)
)
Draw a histogram
The bins parameter specifies the number of bars in the histogram
plt.
hist
(
data
,
bins
=
10
)
Set the figure title and axis labels
plt.
title
(
'RUNOOB hist() Tes'
)
plt.
xlabel
(
'X-Values'
)
plt.
ylabel
(
'Y-Values'
)
Display the figure
plt.
show
(
)
