0

I would like to generate an item in the legend for each color in the scatter plot using the values from class_titles

import matplotlib.pyplot as plt

classes = [0, 1, 2, 0, 1, 1, 2]
x = [0, 1, 2, 0, 1.3, 1.5, 2.2]
y = [.5, 1, 2, .9, 1.2, 1.2, 2.1]

class_titles = ['first', 'second', 'third']

plt.scatter(x, y, c=labels, cmap='gist_rainbow')
plt.show()

scatter plot

Is there any way to do this other than:

  • filtering the X, Y values for each class into 3 separate lists and creating 3 scatter plots

  • load the color map, figure out the color for each label, and create a custom legend from Patches

rob
  • 17,995
  • 12
  • 69
  • 94
  • 1
    Check [this answer](https://stackoverflow.com/a/58516451/4124317) and [this answer](https://stackoverflow.com/a/56507383/4124317) from the duplicates. Also check [the example](https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/scatter_with_legend.html) on the matplotlib page. – ImportanceOfBeingErnest Oct 28 '19 at 16:31
  • Thanks that first answer is exactly what I'm looking for – rob Oct 28 '19 at 16:38

0 Answers0