I have a number of sensors attached to my Raspberry Pi; I\'m sending their data to my PC twice a second using TCP. I would like to continuously graph these values using matp
You could use animation.FuncAnimation. Plot the bar plot once and save the return value, which is a collection of Rects:
rects = plt.bar(range(N), x, align='center')
Then, to change the height of a bar, call rect.set_height
:
for rect, h in zip(rects, x):
rect.set_height(h)
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def animate(frameno):
x = mu + sigma * np.random.randn(N)
n, _ = np.histogram(x, bins, normed=True)
for rect, h in zip(patches, n):
rect.set_height(h)
return patches
N, mu, sigma = 10000, 100, 15
fig, ax = plt.subplots()
x = mu + sigma * np.random.randn(N)
n, bins, patches = plt.hist(x, 50, normed=1, facecolor='green', alpha=0.75)
frames = 100
ani = animation.FuncAnimation(fig, animate, blit=True, interval=0,
frames=frames,
repeat=False)
plt.show()
If matplotlib is not a forced option, i would recommend a Web Socket based Push System on the server and a Javascript based plotting for the client side. I will list a few advantages first:
Since i am doing something very similar with my Raspberry Pi, i can share my details of the same. It is inspired by this blog post.
The code for server side which pushes the data can be found here. You can probably see that after installing the dependencies, it is very similar to your code and eventually you would find a socket.send()
even in my code.
For the client side, this is the link to the HTML file and this is the JS that gets executed on the browser, which uses Flot Plotting library. I am sure the demo on their home page is awesome enough to be noticed!