如何在图例的同一行上放置多个带有相同标签的符号? [英] How to put multiple symbols with the same label on the same line in the legend?

查看:233
本文介绍了如何在图例的同一行上放置多个带有相同标签的符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个散点图,该散点图由可以是开放点也可以是封闭点的点组成,并可以是四种不同的颜色.如果点是开放的,它将有一个标签.如果关闭,它将有另一个标签.

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.

我想要一个图例,该图例在对应于其标签的一行上并排显示每种颜色的4个点,而不是具有相同标签的每行1个点.

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')

我希望图例输出在一行上的标签旁边具有四个不同的颜色点,而不是每个颜色点将标签重复4次.

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.

推荐答案

您可以将带有点的元组传递给图例,并带有如下所示的点:

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()

这篇关于如何在图例的同一行上放置多个带有相同标签的符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆