matplotlib.axes.Axes.legend无法识别labelcolor作为参数

问题描述

尝试设置图例条目的颜色时遇到麻烦。我要选择 与他们所指的线条颜色相同。在这里,我发布了一个可运行的脚本

import matplotlib.pyplot as plt

x,y = [1,2],[1,2]

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

ax.plot(x,y,label='test',color='r')
ax.legend(labelcolor='r')

plt.show()

那就是我得到的错误

Traceback (most recent call last):
  File "test.py",line 11,in <module>
    ax.legend(labelcolor='r')
  File "/home/username/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_axes.py",line 406,in legend
    self.legend_ = mlegend.Legend(self,handles,labels,**kwargs)
TypeError: __init__() got an unexpected keyword argument 'labelcolor'

但是我在legend documentation中看到labelcolor应该作为参数。 你有什么建议吗?

提前谢谢

解决方法

将matplotlib升级到version 3.3.0

然后再试一次。

import matplotlib.pyplot as plt

x,y = [1,2],[1,2]

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

ax.plot(x,y,label='test',color='r')
ax.legend(title='Guide',labelcolor='red')

plt.show()

enter image description here