如何更改pylab图例的颜色?
如何改变图表中不同图例的颜色?我需要在“pylab.legend”的同一行中做到这一点,因为这是我自己代码的问题...
举个例子:
pylab.legend (("Legend1", "Legend2")) #and more legends....
我怎么才能在同一行上把Legend1设置成红色,Legend2设置成蓝色呢?我试过这个:
pylab.legend (("Legend1", 'r', "Legend2 ',' b '))
但是不行
1 个回答
0
想要改变图例线的颜色?我觉得这样做没什么意义。如果你这么做了,怎么能分清楚哪条线代表哪个呢?
如果你想改变文字的颜色,让文字的颜色和对应的线条颜色一致,可以使用以下代码:
plt.plot(range(10), 'r', label='L1')
plt.plot(range(1,11), 'b', label='L2')
L=plt.legend(loc=0)
_=[item.set_color(jtem.get_color()) for item, jtem in zip(L.get_texts(), L.get_children()[1:])]
如果你想把它们都改成,比如说 ['红色', '蓝色']
:
_=[(item.set_color(c), jtem.set_color(c))
for item, jtem, c in zip(L.get_texts(), L.get_children()[1:], ['red', 'blue'])]