w3resource

Pandas Series: plot.hist() function

Series-plot.hist() function

A histogram is a representation of the distribution of data. This function groups the values of all given Series in the DataFrame into bins and draws all bins in one matplotlib.axes.Axes. This is useful when the DataFrame’s Series are in a similar scale.

The plot.hist() function is used to draw one histogram of the DataFrame’s columns.

Syntax:

Series.plot.hist(self, by=None, bins=10, **kwargs)

Parameters:

Name Description Type/Default Value Required / Optional
by Column in the DataFrame to group by. str or sequence Optional
bins Number of histogram bins to be used. int, default 10 Required
**kwds Additional keyword arguments are documented in DataFrame.plot()   Optional

Returns: class:matplotlib.AxesSubplot Return a histogram plot.

Example - When we draw a dice 8000 times, we expect to get each value around 2000 times. But when we draw two dices and sum the result, the distribution is going to be quite different. A histogram illustrates those distributions:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame(
     np.random.randint(1, 8, 8000),
     columns = ['one'])
df['two'] = df['one'] + np.random.randint(1, 8, 8000)
ax = df.plot.hist(bins=12, alpha=0.5)

Output:

Pandas Series: plot.hist() function

Previous: Series-plot.density() function
Next: Series-plot.kde() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/pandas/series/series-plot-hist.php