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

2024-09-27 00:22:23 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

先谢谢你


Tags: inpytestselfmatplotlib颜色linefig
1条回答
网友
1楼 · 发布于 2024-09-27 00:22:23

将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

相关问题 更多 >

    热门问题