简体   繁体   中英

Live updating plot python

I'd like to make live updating graph in python. I've done something like this:

import matplotlib.pyplot as plt
import os

plt.ion()
x = []
y = []
home = os.sep.join((os.path.expanduser('~'), 'Desktop'))


for i in range(-350,350):
    x.append(i)
    y.append(i*i)

    plt.plot(x, y, 'g-', linewidth=1.5, markersize=4)

    plt.show()
    plt.pause(0.1)
plt.pause(5)

plt.savefig(os.path.join(home, 'nowy', '2.png')) 

And it works but I've wonderd if there is a better lib for this? This one is to slow. And if there is some way to make an X line from 0 to 200 and Y line would update while getting new points?

Instead of plotting a new line in every iteration you could solely change the values of the first line-plot. This should lead to a noteable perfomance boost.

import matplotlib.pyplot as plt
import os

plt.ion()
line_handle = plt.plot(0, 0, 'g-', linewidth=1.5, markersize=4)  # create plot handle

x = []
y = []
home = os.sep.join((os.path.expanduser('~'), 'Desktop'))


for i in range(-350, 350):
    x.append(i)
    y.append(i*i)
    line_handle.set_ydata(y)  # change values instead of drawing another line
    line_handle.set_xdata(x)
    plt.draw()
    plt.show()
    plt.pause(0.1)
plt.pause(5)

plt.savefig(os.path.join(home, 'nowy', '2.png'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM