How to Create a Scatter Plot in Matplotlib

Spread the love

Scatter Plot –

Read a dataset to work with.

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 matplotlib, we use the plt.scatter() method or plt.plot() method.

Let’s say I want to plot Strike Rate (SR) on the x axis and Runs on the y axis.

plt.scatter() –

plt.figure(figsize=(10, 8))
plt.scatter(x=df['SR'], y=df['Runs'])
plt.xlabel("Strike Rate")
plt.ylabel("Runs Scored")
plt.title("Runs vs Strike Rate")
plt.show()

plt.plot() –

To use plt.plot() to create scatter plot you need to set the marker of the plot method.

plt.figure(figsize=(10, 8))
plt.plot(df['SR'],df['Runs'], 'o')
plt.xlabel("Strike Rate")
plt.ylabel("Runs Scored")
plt.title("Runs vs Strike Rate")
plt.show()

Color –

You can also add colors to the points to distinguish between different groups.

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

plt.figure(figsize=(10, 8))
plt.scatter(x=indian['SR'], y=indian['Runs'], color="seagreen")
plt.scatter(x=overseas['SR'], y=overseas['Runs'], color="blue")
plt.xlabel("Strike Rate")
plt.ylabel("Runs Scored")
plt.title("Runs vs Strike Rate")
plt.show()

Size –

plt.figure(figsize=(10, 8))
plt.scatter(x=indian['SR'], y=indian['Runs'], color="seagreen", s=indian['HS'])
plt.scatter(x=overseas['SR'], y=overseas['Runs'], color="blue", s=overseas['HS'])
plt.xlabel("Strike Rate")
plt.ylabel("Runs Scored")
plt.title("Runs vs Strike Rate")
# plt.savefig("mat_scatter3.png")
plt.show()

Transparency –

You can also add transparency to your data using the alpha parameter.

plt.figure(figsize=(10, 8))
plt.scatter(x=indian['SR'], y=indian['Runs'], color="seagreen", s=indian['HS'], alpha=0.5)
plt.scatter(x=overseas['SR'], y=overseas['Runs'], color="blue", s=overseas['HS'], alpha=0.5)
plt.xlabel("Strike Rate")
plt.ylabel("Runs Scored")
plt.title("Runs vs Strike Rate")
plt.show()

Colorbar –

You can also add a colorbar to your plot using the cmap parameter and add the plt.colorbar() outside of the plt.scatter()

plt.figure(figsize=(10, 8))
plt.scatter(x=indian['SR'], y=indian['Runs'], s=indian['HS'], alpha=0.5, cmap='virdis')
plt.scatter(x=overseas['SR'], y=overseas['Runs'], s=overseas['HS'], alpha=0.5, cmap="virdis")
plt.colorbar()
plt.xlabel("Strike Rate")
plt.ylabel("Runs Scored")
plt.title("Runs vs Strike Rate")
plt.show()

Marker Style –

To change the marker style, use the marker parameter. You can find all marker types here – marker style

plt.figure(figsize=(10, 8))
plt.scatter(x=indian['SR'], y=indian['Runs'], s=indian['HS'], marker='^')
plt.scatter(x=overseas['SR'], y=overseas['Runs'], s=overseas['HS'], alpha=0.5)
plt.xlabel("Strike Rate")
plt.ylabel("Runs Scored")
plt.title("Runs vs Strike Rate")
plt.savefig("mat_scatter7.png")
plt.show()

Rating: 1 out of 5.

Leave a Reply