Python 从图中删除错误条和线

Python 从图中删除错误条和线,python,matplotlib,Python,Matplotlib,我想将三个绘图保存到一个文件中。在第一个文件中,应包含所有三个绘图,在第二个文件中仅包含两个绘图,在第三个文件中仅包含一个绘图。 我的想法如下: import matplotlib.pyplot as plt line1 = plt.plot([1,2,3],[1,2,3]) line2 = plt.plot([1,2,3],[1,6,18]) line3 = plt.plot([1,2,3],[1,1,2]) fig.savefig("testplot1.png") line1[0].remo

我想将三个绘图保存到一个文件中。在第一个文件中,应包含所有三个绘图,在第二个文件中仅包含两个绘图,在第三个文件中仅包含一个绘图。 我的想法如下:

import matplotlib.pyplot as plt
line1 = plt.plot([1,2,3],[1,2,3])
line2 = plt.plot([1,2,3],[1,6,18])
line3 = plt.plot([1,2,3],[1,1,2])
fig.savefig("testplot1.png")
line1[0].remove()
fig.savefig("testplot2.png")
line2[0].remove()
fig.savefig("testplot3.png")
现在,这个很好用。问题是我想使用errorbars。所以我试着:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
line1=ax.errorbar([1,2,3],[1,2,3],yerr=[0.2,0.2,0.2])
line2=ax.errorbar([1,2,3],[1,6,18],yerr=[0.2,0.2,0.2])
line3=ax.errorbar([1,2,3],[1,1,2],yerr=[0.2,0.2,0.2])
fig.savefig("testplot1.png")
line1[0].remove()
fig.savefig("testplot2.png")
line2[0].remove()
fig.savefig("testplot3.png")
现在,线仍然被移除,但错误条仍然存在。我不知道如何删除errorbar的所有部分。有人能帮我吗?

返回三件事:

  • 绘图线(您的数据点)
  • 封口线(错误条的封口)
  • 条线(显示错误条的条线)
您需要将它们全部删除才能完全“删除”绘图


请注意,您必须迭代第2个和第3个参数,因为它们实际上是对象列表。

似乎ErrorbarContainer.remove方法应该相应更新是的,看起来您应该能够使用
line1.remove()
,但由于其中的对象是元组,因此它们没有
remove
方法。
import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_subplot(111)

line1=ax.errorbar([1,2,3],[1,2,3],yerr=[0.2,0.2,0.2])
line2=ax.errorbar([1,2,3],[1,6,18],yerr=[0.2,0.2,0.2])
line3=ax.errorbar([1,2,3],[1,1,2],yerr=[0.2,0.2,0.2])

fig.savefig("testplot1.png")

line1[0].remove()
for line in line1[1]:
    line.remove()
for line in line1[2]:
    line.remove()

fig.savefig("testplot2.png")

line2[0].remove()
for line in line2[1]:
    line.remove()
for line in line2[2]:
    line.remove()

fig.savefig("testplot3.png")