图例中的颜色与图形颜色不匹配

时间:2019-05-21 08:04:37

标签: python matplotlib graph

我正在尝试制作仅显示某些线条的自定义图例。但是,图例中的颜色并不总是与绘制线的颜色匹配。在示例中,图例1和图例2很好,但图例3应该显示蓝色和绿色,而不是蓝色和橙色。请参阅附件图像。我怎样才能解决这个问题?

legend 3 should be blue (0) and green (2)

这是我用来生成图像的mwe,但请注意,在实际程序中,绘图功能不能直接访问,也不能从其他文件加载。此外,不应在其他程序中使用该图函数。

import matplotlib.pyplot as plt
import numpy as np

def my_plot_function(x,y,ax):
    #do complicated stuff here, manipulating y for example
    ax.plot(x,y)

x = np.linspace(0,1,100)
fig = plt.figure()
ax = fig.add_subplot(111)

my_plot_function(x, x, ax)
my_plot_function(x, x**2, ax)
my_plot_function(x, x**3, ax)
my_plot_function(x, x**4, ax)

lines = ax.get_lines()
print(lines[0])
print(lines[1])
print(lines[2])
print(lines[3])

fig.legend(lines, labels=range(4), loc=1, title="legend 1")
fig.legend([lines[0],lines[1]], labels=["0","1"], loc=2, title="legend 2")
fig.legend([lines[0],lines[2]], labels=["0","2"], loc=3, title="legend 3")
plt.show()

编辑:
为了澄清我的要求:绘图需要通过my_plot_function进行,label是在单独的文件中定义的,不能进行修改。因此,我无法向其传递其他关键字,例如disabled

4 个答案:

答案 0 :(得分:4)

我对这个问题的处理略有不同。在像您这样的情况下,我总是建议在使用label关键字进行绘图时传递图例条目。然后,要使用选定的图例,可以使用get_legend_handles_labels(),然后将所需的项目传递到fig.legend()。您不必指定handleslabels参数。但是,如果您指定一个(例如您对labels所做的操作),则还应该指定另一个,否则会收到警告

import matplotlib.pyplot as plt
import numpy as np

def plot(x,y,ax):
    ax.plot(x,y)

x = np.linspace(0,1,100)
fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(x, x, label='0')
ax.plot(x, x**2, label='1')
ax.plot(x, x**3, label='2')
ax.plot(x, x**4, label='3')
ax.legend(loc=1, title='legend 1')

h, l = ax.get_legend_handles_labels()

fig.legend([h[0],h[1]], [l[0], l[1]], loc=2, title="legend 2")
fig.legend([h[0],h[2]], [l[0], l[2]], loc=3, title="legend 3")
plt.show() 

enter image description here

下面是回答@gboffi评论的另一种方法,即不使用全局图例提取值的地方。

x = np.linspace(0,1,100)
fig = plt.figure()
ax = fig.add_subplot(111)

l0, = ax.plot(x, x, label='0')
l1, = ax.plot(x, x**2, label='1')
l2, = ax.plot(x, x**3, label='2')
l3, = ax.plot(x, x**4, label='3')

fig.legend([l0, l1], [l0.get_label(), l1.get_label()], loc=2, title="legend 2")
fig.legend([l0, l2], [l0.get_label(), l2.get_label()], loc=3, title="legend 3")
plt.show() 

答案 1 :(得分:1)

运行代码时,我收到legend.py的警告:

UserWarning: You have mixed positional and keyword arguments, some input may be discarded.
  warnings.warn("You have mixed positional and keyword "...

这可能是它不起作用的原因。您需要在图例调用中添加handlers。即

fig.legend(handlers=[lines[0],lines[1]],...

顺便说一句,这是创建图形时分配标签的一种好习惯。

ax.plot(x, y, label='some label')

然后您可以致电

handles, labels = ax.get_legend_handles_labels()

像这样在传说中使用它们

fig.legend(handles=(handles[0],handles[2]), labels=(labels[0],labels[2]),...)

答案 2 :(得分:1)

使用handles关键字参数:

fig.legend(handles=lines, labels=range(4), loc=1, title="legend 1")
fig.legend(handles=[lines[0],lines[1]], labels=["0","1"], loc=2, title="legend 2")
fig.legend(handles=[lines[0],lines[2]], labels=["0","2"], loc=3, title="legend 3")

答案 3 :(得分:1)

在查看Figure.legend的文档时,我们可以看到标签必须与手柄一起使用(反之亦然)。您必须同时放置手柄和标签,或者都不放。

这有效

fig.legend(lines, range(4), loc=1, title="legend 1")
fig.legend((lines[0],lines[1]), ("0","1"), loc=2, title="legend 2")
fig.legend((lines[0],lines[2]), ("0","2"), loc=3, title="legend 3")

这有效

fig.legend(handles=lines, labels=range(4), loc=1, title="legend 1")
fig.legend(handles=(lines[0],lines[1]), labels=("0","1"), loc=2, title="legend 2")
fig.legend(handles=(lines[0],lines[2]), labels=("0","2"), loc=3, title="legend 3")
相关问题