1

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 closed, it will have another label.

I would like a legend where it shows 4 dots of each color side by side on one row corresponding to its label, instead of 1 dot per row with the same label.

import matplotlib.pyplot as plt
import numpy as np

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



plt.scatter(x,y, color = 'blue',  label = 'potatoes')
plt.scatter(x,y, color = 'green', label = 'potatoes')
plt.scatter(x,y, color = 'red', label = 'potatoes')
plt.scatter(x,y, color = 'magenta', label = 'potatoes')

plt.scatter(x,y, color = 'blue',  facecolors='none', label = 'tomatoes')
plt.scatter(x,y, color = 'green',  facecolors='none', label = 'tomatoes')
plt.scatter(x,y, color = 'red',  facecolors='none', label = 'tomatoes')
plt.scatter(x,y, color = 'magenta',  facecolors='none', label = 'tomatoes')

plt.plot(x,y, color = 'blue'    , label= "Florida")
plt.plot(x,y, color = 'green'   , label= "California")
plt.plot(x,y, color = 'red'     , label= "Idaho")
plt.plot(x,y, color = 'magenta' , label= "Montana")

l = plt.legend(handlelength = 0)
llines = l.get_texts()
llines[0].set_color('blue')
llines[1].set_color('green')
llines[2].set_color('red')
llines[3].set_color('magenta')

I would prefer the legend output have the four different color dots next to its label on one row, instead of the label being repeated 4 times with each color dot.

I could change the code to have potatoes and tomatoes just once with a black closed or open dot, but I would prefer it with the four different colored dots on one row if it's possible.

garbanzo
  • 21
  • 1
  • I wrote a `ScatterHandler` [in this answer](https://stackoverflow.com/questions/49297599/why-doesnt-the-color-of-the-points-in-a-scatter-plot-match-the-color-of-the-poi/49301633#49301633) that would do exactly that. Please let me know if we can close this as duplicate or if you want to know something specific about that. – ImportanceOfBeingErnest Jun 27 '19 at 01:40
  • Actually, a precise duplicate is [this one](https://stackoverflow.com/questions/54979672/matplotlib-group-different-scatter-markers-under-the-same-legend). – ImportanceOfBeingErnest Jun 27 '19 at 01:50

1 Answers1

-2

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

Lucas
  • 6,869
  • 5
  • 29
  • 44