Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

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

Continous animation using Python Matplotlib

I'm relatively new to the world of Python but with 15+ years of C++ and Java coding. I'm trying 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()

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

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...