Adding legend based on existing color series

You can create the legend handles using an empty plot with the color based on the colormap and normalization of the scatter plot. import pandas as pd import numpy as np; np.random.seed(1) import matplotlib.pyplot as plt x = [np.random.normal(5,2, size=20), np.random.normal(10,1, size=20), np.random.normal(5,1, size=20), np.random.normal(10,1, size=20)] y = [np.random.normal(5,1, size=20), np.random.normal(5,1, size=20), np.random.normal(10,2, size=20), np.random.normal(10,2, … Read more

Scatter plot with legend for each color in c

First, I have a feeling you meant to use apostrophes, not backticks when declaring colours. For a legend you need some shapes as well as the classes. For example, the following creates a list of rectangles called recs for each colour in class_colours. import matplotlib.patches as mpatches classes = [‘A’,’B’,’C’] class_colours = [‘r’,’b’,’g’] recs = … Read more

How to plot a scatter plot with a legend label for each class

Actually both linked questions provide a way how to achieve the desired result. The easiest method is to create as many scatter plots as unique classes exist and give each a single color and legend entry. import matplotlib.pyplot as plt x=[1,2,3,4] y=[5,6,7,8] classes = [2,4,4,2] unique = list(set(classes)) colors = [plt.cm.jet(float(i)/max(unique)) for i in unique] … Read more