Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

Continuous animation using Python Matplotlib

What is the best way to create a continuous animation using Python Matplotlib? I want to create an oscilloscope kind of live plot of a simulation process that I'm working on. The plot should show data in real time as they are created from the simulation.

So far, I have used the Matplotlib.animation.FuncAnimation class together with sample code from other programmers to understand how it works. The FuncAnimation class apparently creates a limited series of plots (frames) which are saved in memory and can optionally be saved to disk. As seen in the code below, I provide a frames=600 value.

However, I want the animation to be unlimited and constantly be updated at the given interval with the latest data from my simulation as long as it runs. What is the best way of doing that?

Many thanks for your advice!! :)

import matplotlib.pyplot
import matplotlib.animation
import matplotlib.style

x_list = []
y_list = []
speed_m_s = 50
time_s = 0
distance_m = 0
interval_s = 0.05
pause = False

matplotlib.style.use('fivethirtyeight')

#--------------------------------------------------------------------------
# Create a figure and subplot
myFigure = matplotlib.pyplot.figure(figsize=(10, 6), tight_layout=False)
myAxes = matplotlib.pyplot.axes(xlim=(0, 200), ylim=(-50, 50))
#myPlot = myFigure.add_subplot(1,1,1)
line2D_list, = myAxes.plot([], [], lw=2)

def myInitFunc():
    line2D_list.set_data([], [])
    return line2D_list,


#--------------------------------------------------------------------------
# Bind mouse click
def onClick(event):
    global pause
    pause ^= True
    print(pause, ' You pressed: ', event.button, event.xdata, event.ydata)

myFigure.canvas.mpl_connect('button_press_event', onClick)

#--------------------------------------------------------------------------
# Animation function
def myAnimationFunc(i):
    if not pause:
        global x_list
        global y_list
        global speed_m_s
        global time_s 
        global distance_m
        global interval_s

        speed_m_s -= 30 * interval_s
        distance_m += speed_m_s * interval_s
        x_list.append(interval_s * i)
        y_list.append(distance_m)

        line2D_list.set_data(x_list, y_list)
        myAxes.axis([time_s - 10, time_s + 10, -5000, 500])

        return line2D_list, 

#------------------------------------------------------------------------------
# Animation: create and start
myAnimation = matplotlib.animation.FuncAnimation(myFigure, myAnimationFunc, frames=600,
                             init_func=myInitFunc, interval=interval_s * 2, blit=True)

matplotlib.pyplot.show()
question from:https://stackoverflow.com/questions/65835924/continuous-animation-using-python-matplotlib

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...