问题
I have a data stream giving me 125 floats per second and I want to plot them live. At the moment my code looks like this:
Code to read data from stream
counter = 0
while True:
counter = counter+1
data from stream (x values)
In reality the code looks a bit more complicated, of course, but this will make giving advice easier, I think.
I was thinking about just saving the graph as a file:
counter=0
a_data=np.zeros(100,float) #this is limited to 100 floats
while True:
counter = counter+1
bytestring = sock.recv(51) # this is the stream data
raw = struct.unpack(pp,bytestring) # this is the unpacked data
twentyfive = (raw[25]-15310)*0.0265 # this is the x value
a_data[counter] = twentyfive
plt.plot(a_data)
print(twentyfive)
plt.savefig('test.png')
time.sleep(0.01)
The problem is that the data fluctuates a lot so it's way too cluttered to be helpful. The graph should move to the right. In addition it is by no means fast enough. For this reason I was thinking about using pyqtgraph but I have no idea how to feed my x values (125 microvolt values per second) and y values (the time steps as given by the counter) to pyqtgraph in any of the examples I found online so far. Any help would be greatly appreciated.
回答1:
PyQtGraph is a pretty good choice here and it should be no problem to plots 125 samples/second in real time. There are several approaches you could use for plotting real-time, scrolling data and there actually is a good example file in PyQtGraph showing just that: https://github.com/pyqtgraph/pyqtgraph/blob/develop/examples/scrollingPlots.py
You can run the example by running this in your Python interpreter after installing PyQtGraph:
import pyqtgraph.examples
pyqtgraph.examples.run()
and selecting the "Scrolling plots" example.
来源:https://stackoverflow.com/questions/29213291/realtime-plotting-in-python