using matplotlib or pyqtgraph to graph real time data

后端 未结 4 1095
挽巷
挽巷 2020-12-09 23:44

I have devices connected to my serial port and I need to poll them and then display that data in a plot. I currently have this working (slowly) using matplotlib. I could h

相关标签:
4条回答
  • 2020-12-10 00:02

    The pyqtgraph website has a comparison of plotting libraries including matplotlib, chaco, and pyqwt. The summary is:

    • Matplotlib is the de-facto standard plotting library, but is not built for speed.
    • Chaco is built for speed but is difficult to install / deploy
    • PyQwt is currently abandoned
    • PyQtGraph is built for speed and easy to install
    0 讨论(0)
  • 2020-12-10 00:10

    Here is a way to do it using the animation function:

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    
    fig, ax = plt.subplots()
    data = np.zeros((32,100))
    X = np.arange(data.shape[-1])
    
    # Generate line plots
    lines = []
    for i in range(len(data)):
        # Each plot each shifter upward
        line, = ax.plot(X,i+data[i], color=".75")
        lines.append(line)
    
    # Set limits
    ax.set_ylim(0,len(data))
    ax.set_xlim(0,data.shape[-1]-1)
    
    # Update function
    def update(*args):
        # Shift data left
        data[:,:-1] = data[:,1:]
    
        # Append new values
        data[:,-1] = np.arange(len(data))+np.random.uniform(0,1,len(data))
    
        # Update data
        for i in range(len(data)):
            lines[i].set_ydata(data[i])
    
    ani = animation.FuncAnimation(fig, update,interval=10)
    plt.show()
    
    0 讨论(0)
  • 2020-12-10 00:20

    I've used matplotlib and PyQtGraph both extensively and for any sort of fast or 'real time' plotting I'd STRONGLY recommend PyQtGraph, (in one application I plot a data stream from an inertial sensor over a serial connection of 12 32-bit floats each coming in at 1 kHz and plot without noticeable lag.)

    As previous folks have mentioned, installation of PyQtGraph is trivial, in my experience it displays and performs on both windows and linux roughly equivalently (minus window manager differences), and there's an abundance of demo code in the included examples to guide completion of almost any data plotting task.

    The web documentation for PyQtGraph is admittedly less than desirable, but the source code is well commented and easy to read, couple that with well documented and diverse set of demo code and in my experience it far surpasses matplotlib in both ease of use and performance (even with the much more extensive online documentation for matplotlib).

    0 讨论(0)
  • 2020-12-10 00:23

    I would suggest Chaco "... a package for building interactive and custom 2-D plots and visualizations." It can be integrated in Qt apps, though you can probably get higher frame rates from PyQwt.

    I've actually used it to write an "app" (that's too big a word: it's not very fancy and it all fits in ~200 LOC) that gets data from a serial port and draws it (20 lines at over 20 fps, 50 at 15 fps, at full screen in my laptop).

    Chaco documentation or online help weren't as comprehensive as matplotlib's, but I guess it will have improved and at any rate it was enough for me.

    As a general advice, avoid drawing everything at every frame, ie., use the .set_data methods in both matplotlib and chaco. Also, here in stackoverflow there are some questions about making matplotlib faster.

    0 讨论(0)
提交回复
热议问题