如何获取matplotlib中最后一个数字的颜色?

Yot*_*tam 12 python colors matplotlib

我正在绘制一些线性拟合的数据集.我希望线性拟合具有与绘制数据相同的颜色(误差条).我怎样才能得到那种颜色?

bte*_*tel 18

你可以试试这个:

x = np.arange(10)
y = np.arange(10)
err = np.ones(10)
ebar = plt.errorbar(x,y, yerr=err)
color = ebar[0].get_color()
Run Code Online (Sandbox Code Playgroud)

ebar 是艺术家的容器,因此您可以修改最后一行中的索引以匹配您想要获得颜色的艺术家.

您还可以轻松设置错误栏的颜色,这样您就可以确切地知道它们的颜色而无需检查它:

ebar = plt.errorbar(x,y, yerr=err, ecolor='y')
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你的回答!同样的技巧也适用于plt.plot(...).我之所以提到这一点,是因为我发现这个答案很有帮助,但并没有考虑使用误差线,而是让三条曲线颜色匹配. (2认同)