Skip to content Skip to sidebar Skip to footer

How To Put Multiple Symbols With The Same Label On The Same Line In The Legend?

I am making a scatter plot which is made up of dots that can be either open or closed dots, and can be in four different colors. If the dot is open, it will have one label. If it's

Solution 1:

You can pass a tuple with the points to legend with the points like this:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerTuple

x = np.arange(3)
y = np.arange(3)

p1 = plt.scatter(x,y, color = 'blue')
p2 = plt.scatter(x,y, color = 'green')
p3 = plt.scatter(x,y, color = 'red')
p4 = plt.scatter(x,y, color = 'magenta')

t1 = plt.scatter(x,y, color = 'blue',  facecolors='none')
t2 = plt.scatter(x,y, color = 'green',  facecolors='none')
t3 = plt.scatter(x,y, color = 'red',  facecolors='none')
t4 = plt.scatter(x,y, color = 'magenta',  facecolors='none')

plt.legend([(p1, p2, p3, p4), (t1, t2, t3, t4)], ['potatoes', 'tomatoes'],
           scatterpoints=1, numpoints=1, handler_map={tuple: HandlerTuple(ndivide=None)})
plt.show()

example_legend

Post a Comment for "How To Put Multiple Symbols With The Same Label On The Same Line In The Legend?"