Introduction
Matplotlib is a well-know and powerful visualization library in Python, you must deal with this library so frequently as a Python developer and we are now going to explore some deeper phase of this library.
Because of my previous project-Python OPC Connector, I’ve got demand to make real time plots to represent acquired data. The real-time plotting is achieved by using FuncAnimation in matplotlib. The FuncAnimation will call certain plotting function and pass on variables periodically to update the plot content. Below we will show you sample code and how each part of the code functions.
Sample Code
Initiation and Prerequsite
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# Activate interactive plotting in Jupyter Notebook
%matplotlib notebook
#Initiate figure and ax objects
fig, ax = plt.subplots()
x, y = [], []
line, = ax.plot([], [], 'bo')
The very first step is to import necessary packages. We will utilize the plt function as well as FuncAnimation. If you are working in Jupyter environment. The “%matplotlib notebook” part is mandatory to make interactive plot work.
The third part is to initiate figure object and plot object.
Main Body
def init():
"""
This init function defines the initial plot parameter
"""
# Set initial parameter for the plot
ax.set_xlim(0, 100)
ax.set_ylim(-2.5, 2.5)
return line,
def animate(frame):
"""
This function will be called periodically by FuncAnimation. Frame parameter will be passed on each call as a counter.
"""
# Append data to x and y data list
x.append(frame)
y.append(sim_data_node_sin.get_value())
# Adjust limit when step count exceeds certain number (100 in this case)
limit = max(100, max(x))
ax.set_xlim(limit-100, limit)
# Set data for line plot
line.set_data(x, y)
return line,
# Create FuncAnimation object and plt.show() to show the updated animation
ani = FuncAnimation(fig, animate, frames = np.linspace(1,1000,1000), interval = 1000, init_func = init)
plt.show(
You can pass on init function if exists to FuncAnimation. Usually it sets some default parameter to the plot.
The animate function has one input called frame. This is the input that FuncAnimation will pass on to the function for each run. Basically the frame variable is something that causes the plot value to change, it’s like time and the animate function is a function of time.
In this case, frame acts like count and the desired value was retrieved from OPC simulation server for each run. After either calculating or retrieving the value for each run, you may use set_data() method for plot object to update the data.
Finally, you create FuncAnimation object and put fig(figure object), animate(animation function), frame(variable that causes function value to change, in this case we us e linspace to create 1 to 1000), interval(time gap between each function call) and other parameters into FuncAnimation.
Below is the capture of real time plot of sinusoidal function.
Conclusion
In this short article, we have demonstrated sample code of creating and configuring real time plot with FuncAnimation. It is not hard to implement a simple but you can create even more complicated and advanced ones on the basis of these basic parameter and functions.
In my previous project, I create connection with OPC server such as matrikon DA server and Prosys UA server and monitor the data transmission with FuncAnimation real time plot. If you are interested in OPC connection using Pythonm you may check out this article and this repo.
If you are interested in more about real time plotting in Python or you have any advice to share, please feel free to contact me via email or social media.