IntroductionSignificant number of users asked about real time plotting examples in Python and tried to use matplotlib for it. Matplotlib is a great library, but its primary focus is offline data. For real time visualization tools like PyQT and Kivy work better. Here we will use pyqtgraph which is built on top of PyQT. Despite the fact that this example is only for Python for now, it shows the basic idea which remains the same across different programming languages and GUI frameworks. Show Also, feel free to check Matthijs blog post about visualization in Julia. Full code for this example can be found here. Installing dependenciesAll you need to install is BrainFlow and pyqtgraph.
Or use requirements.txt file pyqtgraph requires PyQT preinstalled, on unix like systems process of installation is different based on OS. On Fedora it’s as simple as: We recommend to use system package managers like dnf, apt, brew, etc instead installation using pip. Real time plot using pyqtgraphThere are a lot of tutorials about pyqtgraph, we recommend this one. Let’s copypaste code from it:
Getting some data from BrainFlowWe will use brainflow_get_data.py from code samples as a starting point.
Mixing it all togetherWe are one step away from this result.
We will not get data in the
And patch our pyqtgraph part:
Note: here we use Finally, let’s add a title to our plot and some filters.
How do I make a realTo create a real-time plot, we need to use the animation module in matplotlib. We set up the figure and axes in the usual way, but we draw directly to the axes, ax , when we want to create a new frame in the animation.
How do I plot in realTo plot in real-time in a while loop using Python matplotlib, we can create a loop to plot the data and then call pause . to call scatter to plot a scatterplot. Then we call pause to draw the new data and run the GUI's `event loop. And then we call show to show the GUI.
Is Plotly or Matplotlib better?Plotly has several advantages over matplotlib. One of the main advantages is that only a few lines of codes are necessary to create aesthetically pleasing, interactive plots. The interactivity also offers a number of advantages over static matplotlib plots: Saves time when initially exploring your dataset.
|