Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Matplotlib Histogram

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:

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

(

)