【问题标题】:How to remove lines in a Matplotlib animation?如何删除 Matplotlib 动画中的线条?
【发布时间】:2019-05-05 15:47:54
【问题描述】:

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

我的代码引用了this video 如果文本行超过 19 行,则图表被移除。

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

当文本行超过19行时,重新执行matplotlib时,图形会被移除。

我也尝试过使用plt.claplt.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


【解决方案1】:

尝试为行数函数的值分配一个变量,如下所示:

return_value = graph_data.count(' \n')+1

打印此值并在添加行时查看计数功能是否正常工作 希望这可以帮助。

【讨论】:

  • count 是走的,remove()、del 和 plt.cla 中只有 plt.cla 有效。
猜你喜欢
  • 2018-08-10
  • 1970-01-01
  • 1970-01-01
  • 2018-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-27
相关资源
最近更新 更多