This is passing a reference to the function and not the result of that function. Note: As an argument to FuncAnimation, notice that animate does not have any parentheses. Then, we set interval, which is how long we should wait between calls to animate() (in milliseconds). Still in the FuncAnimation() parameters, we set fargs, which are the arguments we want to pass to our animate function (since we are not calling animate() directly from within our own code). We called this function animate() and is defined just above our FuncAnimation() call. We pass the FuncAnimation() a handle to the figure we want to draw, fig, as well as the name of a function that should be called at regular intervals. We set up the figure and axes in the usual way, but we draw directly to the axes, ax, when we want to create a new frame in the animation.Īt the bottom of the code, you'll see the secret sauce to the animation:įuncAnimation is a special function within the animation module that lets us automate updating the graph. To create a real-time plot, we need to use the animation module in matplotlib. Feel free to breathe on the sensor to see how the temperature fluctuates. You should immediately see a graph that gets updated about once every second. # Set up plot to call animate() function periodicallyĪni = animation.FuncAnimation(fig, animate, fargs=(xs, ys), interval=1000) Plt.title('TMP102 Temperature over Time') # This function is called periodically from FuncAnimation Copy in the following code: language:python Open a new file (once again, make sure it's in the same directory as tmp102.py so that we can use the tmp102 module). To accomplish that, we will create an animation where a temperature sample is taken and the graph is updated immediately. Many times, you would like to be able to monitor the output of a sensor in real time, which means you can look for trends as they happen. Waiting to collect measurements from a sensor before plotting it might work in some situations. Share on Twitter Share on Facebook Pin It Update a Graph in Real Time
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |