Categories
Python Answers

How to create scatter by category plots in Python Pandas and Pyplot?

Spread the love

To create scatter by category plots in Python Pandas and Pyplot, we can use the subplots method to do the plots.

For instance, we write

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed(1974)

num = 20
x, y = np.random.random((2, num))
labels = np.random.choice(['a', 'b', 'c'], num)
df = pd.DataFrame(dict(x=x, y=y, label=labels))

groups = df.groupby('label')

# Plot
fig, ax = plt.subplots()
ax.margins(0.05)
for name, group in groups:
    ax.plot(group.x, group.y, marker='o', linestyle='', ms=12, label=name)
ax.legend()

plt.show()

to call np.random.random to create some random data.

And then we convert it to a data frame with DataFrame.

Next, we call plt.subplots to crearte the plot.

Then we loop through the groups we got from groupby and call ax.plot to plot the values.

Next, we call ax.legeng to add a legend.

And then we call plt.show to show the plot.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *