make a scatter plot and color those datapoints based on a class in python

Here's an example of how to create a scatter plot and color the data points based on a categorical variable using matplotlib and pandas.

main.py
import pandas as pd
import matplotlib.pyplot as plt

# create sample data
data = {'x': [1, 2, 3, 4, 5],
        'y': [2, 1, 4, 3, 5],
        'class': ['A', 'B', 'A', 'B', 'A']}
df = pd.DataFrame(data)

# create scatter plot
fig, ax = plt.subplots()
colors = {'A':'red', 'B':'blue'}
ax.scatter(df['x'], df['y'], c=df['class'].apply(lambda x: colors[x]))

# add legend
legend_elements = [plt.Line2D([0], [0], marker='o', color='w', label='A', markerfacecolor='r', markersize=10),
                   plt.Line2D([0], [0], marker='o', color='w', label='B', markerfacecolor='b', markersize=10)]
ax.legend(handles=legend_elements, loc='upper right')

# show plot
plt.show()
668 chars
22 lines

In this example, we have created a Pandas DataFrame with x and y values and a categorical variable called class. We then create a scatter plot using the ax.scatter method and pass in the x, y and color vector c. We create the color vector by applying a lambda function to the class variable which maps the category to the corresponding color from the colors dictionary. Finally, we add a legend to the plot and display it.

This will give you a scatter plot with the data points colored based on the class variable.

gistlibby LogSnag