2

I have data that looks like X_1 = [...], X_2 = [...], Y = [...]. The values in Y are integers representing labels and the values in X_1 and X_2 are floats. I want to make a scatterplot of X_1 vs X_2, and have points coloured in using Y. I can do

plt.scatter(X_1, X_2, c=Y)

but I can't find any documentation describing how to get a legend that shows, for each label in Y, the corresponding colour used for the marker.

How can I create this legend?

1 Answers1

0

To have full control, you can make an empty scatterplot for every color, just to define the markers and labels that are going to appear in the legend:

import matplotlib.pyplot as plt

X_1 = [0.1, 0.5, 0.9]
X_2 = [0.3, 0.7, 0.2]
Y = ['red', 'blue', 'green']

fig, ax = plt.subplots()
for color in Y:   
    ax.scatter([], [], c=color, label=color)

ax.scatter(X_1, X_2, c=Y)

ax.legend()

scatterplot example

Arne
  • 9,990
  • 2
  • 18
  • 28