【问题标题】:matplotlib.axes.Axes.legend does not recognize labelcolor as argumentmatplotlib.axes.Axes.legend 不识别 labelcolor 作为参数
【发布时间】:2020-08-12 16:23:17
【问题描述】:

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

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 应该作为参数。 你有什么建议吗?

提前谢谢你

【问题讨论】:

标签: python matplotlib legend


【解决方案1】:

升级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()

【讨论】:

  • 嗯,我正在尝试更新它(我有 conda 环境),但似乎我无法更新。我尝试使用 conda update conda conda update --all conda install matplotlib=3.3.0 甚至这里建议的命令anaconda.org/conda-forge/matplotlib
  • 你可以尝试使用 pip 来做...... pip install -U matplotlib
  • @F.Addari 不客气。快乐编码。保持安全并保持健康!
猜你喜欢
  • 2012-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多