【发布时间】: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 应该作为参数。 你有什么建议吗?
提前谢谢你
【问题讨论】:
-
检查你的 matplotlib 版本。这是 3.3.0 中的新功能。 (stackoverflow.com/a/63273370/6361531)
-
你的脚本适用于我最新的 Matplotlib。根据这个你得到什么版本的matplotlib:matplotlib.org/3.1.1/faq/…
-
是的,我有 matplotlib 3.1.1。我现在正在尝试更新它
标签: python matplotlib legend