Pandas Plotting – How to Create a Scatter plot in Pandas

Spread the love

Create a Scatter Plot in pandas –

Let’s read a dataset for illustration.

import pandas as pd
url = "https://raw.githubusercontent.com/bprasad26/lwd/master/data/batting.csv"
df = pd.read_csv(url)
df.head()

To create a scatter plot in pandas, we use the DataFrame.plot.scatter() method.

df.plot.scatter(x='SR', y='Runs', figsize=(10, 8));

You can also use DataFrame.plot() method to create a scatter plot, all you have to do is set kind parameter to scatter.

df.plot(x='SR', y='Runs', kind='scatter', figsize=(10, 8));

You can also use the color parameter “c” to distinguish between groups of data.

import matplotlib.pyplot as plt

indian = df[df['Nationality']=='Indian']
overseas = df[df['Nationality']=='Overseas']

fig, ax = plt.subplots(figsize=(10, 8))
indian.plot.scatter(x='SR', y='Runs', c='seagreen',label='Indian', ax=ax)
overseas.plot.scatter(x='SR', y='Runs',c='crimson',label='Overseas', ax=ax)
plt.legend()
plt.show()

You can also create subplots, this will plot different groups in different plots.

import matplotlib.pyplot as plt

indian = df[df['Nationality']=='Indian']
overseas = df[df['Nationality']=='Overseas']

fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(10,8))
indian.plot.scatter(x='SR', y='Runs', c='seagreen',label='Indian', ax=ax1)
overseas.plot.scatter(x='SR', y='Runs',c='crimson',label='Overseas', ax=ax2)
plt.legend()
plt.show()

Leave a Reply