This can be usefull when you want to visualize incoming data in real-time. This data could, for example, come from a microcontroller that is continuously sampling an analog signal.
In this example we will get our data from a named pipe (also known as a fifo). For this example, the data in the pipe should be numbers separted by newline characters, but you can adapt this to your liking.
Example data:
100
123.5
1589
More information on named pipes
We will also be using the datatype deque, from the standard library collections. A deque object works quite a lot like a list. But with a deque object it is quite easy to append something to it while still keeping the deque object at a fixed length. This allows us to keep the x axis at a fixed length instead of always growing and squishing the graph together. More information on deque objects
Choosing the right backend is vital for performance. Check what backends work on your operating system, and choose a fast one. For me only qt4agg and the default backend worked, but the default one was too slow. More information on backends in matplotlib
This example is based on the matplotlib example of plotting random data.
None of the ' characters in this code are meant to be removed.
import matplotlib
import collections
#selecting the right backend, change qt4agg to your desired backend
matplotlib.use('qt4agg')
import matplotlib.pyplot as plt
import matplotlib.animation as animation
#command to open the pipe
datapipe = open('path to your pipe','r')
#amount of data to be displayed at once, this is the size of the x axis
#increasing this amount also makes plotting slightly slower
data_amount = 1000
#set the size of the deque object
datalist = collections.deque([0]*data_amount,data_amount)
#configure the graph itself
fig, ax = plt.subplots()
line, = ax.plot([0,]*data_amount)
#size of the y axis is set here
ax.set_ylim(0,256)
def update(data):
line.set_ydata(data)
return line,
def data_gen():
while True:
"""
We read two data points in at once, to improve speed
You can read more at once to increase speed
Or you can read just one at a time for improved animation smoothness
data from the pipe comes in as a string,
and is seperated with a newline character,
which is why we use respectively eval and rstrip.
"""
datalist.append(eval((datapipe.readline()).rstrip('\n')))
datalist.append(eval((datapipe.readline()).rstrip('\n')))
yield datalist
ani = animation.FuncAnimation(fig,update,data_gen,interval=0, blit=True)
plt.show()
If your plot starts to get delayed after a while, try adding more of the datalist.append data, so that more lines get read each frame. Or choose a faster backend if you can.
This worked with 150hz data from a pipe on my 1.7ghz i3 4005u.