18

在某些 LCD 显示器上,图例中水平线的颜色很难区分。(见附图)。因此,不是在图例中画一条线,而是可以只对文本本身进行颜色编码吗?所以换句话说,蓝色的“y = 0x”,绿色的“y = 1x”等等......

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。如果仅在图例中可以使线条变粗,但在情节中却不能,这也可以。

4

4 回答 4

23

I was wondering the same thing. Here is what I came up with to change the color of the font in the legend. I am not totally happy with this method, since it seems a little clumsy, but it seems to get the job done [Edit: see below for a better way]:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)

fig = plt.figure()
ax = plt.subplot(111)

colors = []
for i in xrange(5):
    line, = ax.plot(x, i * x, label='$y = %ix$' % i)
    colors.append(plt.getp(line,'color'))

leg = ax.legend()

for color,text in zip(colors,leg.get_texts()):
    text.set_color(color)

plt.show()

the colorful results

2016 Edit:

Actually, there is a better way. You can simply iterate over the lines in the legend, which avoids needing to keep track of the colors as the lines are plotted. Much less clunky. Now, changing the line colors is basically a one-liner (okay, it's actually a two-liner). Here is the complete example:

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)

leg = ax.legend()

# change the font colors to match the line colors:
for line,text in zip(leg.get_lines(), leg.get_texts()):
    text.set_color(line.get_color())

plt.show()

2017 Edit: Lastly, if you really do want the color-coded text instead of a line (as the title suggests), then you can suppress the lines in the legend by using

 leg = ax.legend(handlelength=0)
于 2013-08-27T23:19:16.230 回答
9

只需设置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)
   ....:     

legend_linewidth.png

于 2012-12-11T21:35:44.473 回答
8

在通过图例文本 getter/setter 和轴线 getter/setter 完成所有绘图之后,可以干净地完成此操作。在绘图之前将图例文本颜色设置为与 for 循环中的线条颜色相同。

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)

leg = ax.legend()

def color_legend_texts(leg):
    """Color legend texts based on color of corresponding lines"""
    for line, txt in zip(leg.get_lines(), leg.get_texts()):
        txt.set_color(line.get_color())  

color_legend_texts(leg)    

plt.show()

在这个答案中要注意的主要区别是格式化绘图可以与绘图操作完全分离。

于 2014-01-16T12:37:12.797 回答
2

提供更通用的解决方案来使用图例句柄的颜色为图例文本着色:这不仅适用于线条,而且适用于图例中的任何艺术家。它看起来如下:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)

fig = plt.figure()
ax = plt.subplot(111)


ax.plot(x, 3 * x, label='$y = %ix$' % 3)
ax.scatter(x, 4 * x, color="red", label='$y = %ix$' % 4)
ax.hist(x, label="hist")
ax.errorbar(x,2*x,yerr=0.3*x, label='$y = %ix$' % 2)

leg = ax.legend()

for artist, text in zip(leg.legendHandles, leg.get_texts()):
    try:
        col = artist.get_color()
    except:
        col = artist.get_facecolor()
    if isinstance(col, np.ndarray):
        col = col[0]
    text.set_color(col)

plt.show()

在此处输入图像描述

于 2018-05-03T19:57:04.213 回答