Matplotlib:图例中用颜色编码的文本,而不是一行 [英] Matplotlib: Color-coded text in legend instead of a line
本文介绍了Matplotlib:图例中用颜色编码的文本,而不是一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在某些LCD监视器上,图例中水平线的颜色很难区分. (请参见附件).因此,除了在图例中画一条线以外,是否可以仅对文本本身进行颜色编码?所以换句话说,蓝色为"y = 0x",绿色为"y = 1x",等等...
On certain LCD monitors, the color of the horizontal lines in the legend is hard to tell apart. (See the image attached). So instead of drawing a line in the legend, is it possible to just color code the text itself? so another words, have "y=0x" in blue, "y=1x" in green, etc...
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
for i in xrange(5):
ax.plot(x, i * x, label='$y = %ix$' % i)
ax.legend()
plt.show()
PS.如果可以仅在图例中而不是在情节中将线变粗,这也将起作用.
PS. if the line could be made thicker just in the legend, but not in the plot, this would also work.
推荐答案
只需设置图例句柄的linewidth
:
In [55]: fig, ax = plt.subplots()
In [56]: x = np.arange(10)
In [57]: for i in xrange(5):
....: ax.plot(x, i * x, label='$y = %ix$' % i)
....:
In [58]: leg = ax.legend(loc='best')
In [59]: for l in leg.legendHandles:
....: l.set_linewidth(10)
....:
这篇关于Matplotlib:图例中用颜色编码的文本,而不是一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文