Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何删除Matplotlib动画中的线?_Python_Python 3.x_Matplotlib_Graph - Fatal编程技术网

Python 如何删除Matplotlib动画中的线?

Python 如何删除Matplotlib动画中的线?,python,python-3.x,matplotlib,graph,Python,Python 3.x,Matplotlib,Graph,我要删除动画中的图形, 当文本行超过19行时 我的代码引用了 如果文本行超过19行,则图形将被删除 我使用了remove()和del,但这不起作用,因为在此代码中使用remove()和del不会自动删除图形 当文本行超过19行时,将在重新执行matplotlib时删除图形 我也试过使用plt.cla,plt.clf,但是这个东西。。 也移除标签。。我不想删除标签和标题 我怎样才能修好它 这是我的密码: fig = plt.figure() ax1 = fig.add_subplot(1,1,1)

我要删除动画中的图形, 当文本行超过19行时

我的代码引用了 如果文本行超过19行,则图形将被删除

我使用了
remove()
del
,但这不起作用,因为在此代码中使用
remove()
del
不会自动删除图形

当文本行超过19行时,将在重新执行matplotlib时删除图形

我也试过使用
plt.cla
plt.clf
,但是这个东西。。 也移除标签。。我不想删除标签和标题

我怎样才能修好它

这是我的密码:

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
plt.xlim(-190,190)
plt.ylim(-190,190)

def animate(i):
    graph_data = open('data.txt', 'r').read()    
    lines = graph_data.split('\n') 
    xs = []
    ys = []

    for line in lines:
        if len(line) > 1:
            x, y = line.split(',') 
            xs.append(x)
            ys.append(y)

    ax1.plot(xs, ys, 'r')

    if (graph_data.count(' \n')+1) >=19: 
       ax1.lines[0].remove() 
       ##del ax1.lines[0] 
       ##plt.cla()

ani = animation.FuncAnimation(fig, animate,interval=1, frames=2, repeat=True)
plt.show()

尝试将变量指定给lines count函数的值,如中所示:

return_value = graph_data.count(' \n')+1
打印此值,并在添加行时查看计数函数是否正常工作
希望这有帮助

count正在行走,在remove()、del和plt.cla之间,只有plt.cla起作用。您解决过这个问题吗?我也有同样的问题。