

You might notice that the plot updates only once per second (as defined by interval=1000). Similarly, we need to reformat the plot for each frame. If we didn't clear them each time, plots would just be drawn on top of each other, and the whole graph would be a mess. In order to draw the plot, we must clear the axes with ax.clear() and then plot the line with ax.plot(). If we let the lists grow indefinitely, the timestamps would be hard to read, and we would eventually run our of memory. We also truncate both xs and ys to keep them limited to 20 elements each. Within animate(), we collect the temperature data and append a timestamp, just like in the previous example. Without explicitly saying we want xs and ys as parameters, we would need to use global variables for remembering the values in xs and ys. We told FuncAnimation() that we wanted to pass in xs and ys with the fargs parameter. xs and ys are our lists containing a timestamp and temperature values, respectively. Whenever animate() is called, i will be automatically incremented by 1. This parameter is necessary when defining a function for FuncAnimation. If you look at the call to animate(), you'll see that it has 3 parameters that we've defined: If you accidentally add parentheses to animate here, animate will be called immediately (only once), and you'll likely get an error (probably something about a tuple not being callable)! 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.
#Cplot figure handle free
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.
#Cplot figure handle update
Share on Twitter Share on Facebook Pin It Update a Graph in Real Time
