Python 如何删除Matplotlib动画中的线?
我要删除动画中的图形, 当文本行超过19行时 我的代码引用了 如果文本行超过19行,则图形将被删除 我使用了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)
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起作用。您解决过这个问题吗?我也有同样的问题。