更新 matplotlib 中的行

2023-11-23

我有一个包含多个数据集的图表。随着数据的更新,我需要不断地重新绘制这些线,每条线都是单独的。如何重复删除并重新建立它,最好不必每次都删除整个图表并重新绘制其上的所有线条?


#!/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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

更新 matplotlib 中的行 的相关文章

随机推荐