在 matplotlib 中更新行 [英] Update Lines in matplotlib

查看:79
本文介绍了在 matplotlib 中更新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个数据集的图形.随着数据的更新,我需要不断地分别重绘这些行.我该如何反复删除并重新建立它,而不必每次都删除整个图并在其上重新绘制所有线条?

I have a graph with multiple data sets on it. I need to continually redraw these lines, each separately, as the data is updated. How can I delete and reestablish it repeatedly, preferably without having to delete the entire graph and redraw all of the lines on it every time?

推荐答案

#!/usr/bin/env python

import time
from pylab import *

ion() # turn interactive mode on

# initial data
x = arange(-8, 8, 0.1);
y1 = sin(x)
y2 = cos(x)

# initial plot
line1, line2, = plot(x, y1, 'r', x, y2, 'b')
line1.axes.set_xlim(-10, 10)
line1.axes.set_ylim(-2, 2)
line1.set_label("line1")
line2.set_label("line2")
legend()
grid()
draw()

# update line 1
for i in xrange(50):
    time.sleep(0.1)

    # update data
    y1 = sin(x + float(i) / 10)

    # update plot
    line1.set_ydata(y1)
    draw()

# update line 2
for i in xrange(50):
    time.sleep(0.1)

    # update data
    y2 = cos(x + float(i) / 10)

    # update plot
    line2.set_ydata(y2)
    draw()

这篇关于在 matplotlib 中更新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆