Matplotlib图例颜色与图色不同-错误还是错误?

时间:2018-08-02 07:35:49

标签: python-2.7 matplotlib legend errorbar jupyter-lab

我用误差线绘制数据,并希望在图例中标记它们。但是,图例中的颜色与图中的颜色不匹配。

玩具示例:

import matplotlib.pyplot as plt
plt.figure()
plt.plot(np.arange(50),label='data')
plt.errorbar(np.arange(50),np.arange(50),yerr=np.arange(50),label='errors')
plt.legend()

resulting plot

此行为是错误还是我做错了什么?

谢谢!

我正在使用python 2.7和jupyter实验室

2 个答案:

答案 0 :(得分:1)

ploterrorbar都有一行。一个隐藏另一个。

您可以将错误栏的linestyle设置为"None"

import numpy as np
import matplotlib.pyplot as plt

plt.figure()
plt.plot(np.arange(50),label='data')
plt.errorbar(np.arange(50),np.arange(50),yerr=np.arange(50),ls="None",
             label='errors')
plt.legend()

plt.show()

enter image description here

您还可以将图的zorder设置为更高的数字,以使其出现在前面。

enter image description here

答案 1 :(得分:1)

您可能还认为plt.errorbar()有两个颜色参数:

import numpy as np
import matplotlib.pyplot as plt
plt.figure()
plt.plot(np.arange(50),color='blue',label='data')
plt.errorbar(np.arange(0,50,2),np.arange(0,50,2),yerr=np.arange(0,50,2),color='blue',ecolor='red',label='errors')
plt.legend()
plt.show()

enter image description here