【发布时间】:2019-05-05 15:47:54
【问题描述】:
我想删除动画中的图形, 当文本行超过 19 行时..
我的代码引用了this video 如果文本行超过 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()
【问题讨论】:
-
你解决过这个问题吗?我有同样的问题。
标签: python python-3.x matplotlib graph