Pandas Plotting – How to Create a Histogram in Pandas

Spread the love

Create Histogram in pandas –

Read a dataset to work with.

import pandas as pd
url = ""
df = pd.read_csv(url)

To create a Histogram in pandas we use the DataFrame.plot.hist() method.

df.plot.hist(y='Runs', figsize=(10,8))

If you want to change the number of bins, you can do so using the bins parameter.

df.plot.hist(y='Runs', bins=20, figsize=(10,8));

To change the color of the histogram, use the color parameter.

df.plot.hist(y='Runs', bins=20, color='seagreen', figsize=(10,8));

Comparing groups –

If you want, You can also create multiple histogram on the same figure.

fig, ax = plt.subplots(figsize=(10, 8))
indian.plot.hist(y='Runs', bins=20, color='seagreen', label='Indian', ax=ax)
overseas.plot.hist(y='Runs', bins=20, color='crimson', label='Overseas', ax=ax);

Subplots –

Instead of plotting multiple histogram on the same plot, you can plot each histogram separately in their own axes.

fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(12, 7))
indian.plot.hist(y="Runs", bins=20, color="seagreen", label="Indian", ax=ax1)
overseas.plot.hist(y="Runs", bins=20, color="crimson", label="Overseas", ax=ax2)

Rating: 1 out of 5.

Leave a Reply