基于numpy 、pyqt5、pyside2的纯python图形界面库。
pyqtgraph
Introduction
What is pyqtgraph?
PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in engineering and science applications. Its primary goals are 1) to provide fast, interactive graphics for displaying data (plots, video, etc.) and 2) to provide tools to aid in rapid application development (for example, property trees such as used in Qt Designer).
PyQtGraph makes heavy use of the Qt GUI platform (via PyQt or PySide) for its high-performance graphics and numpy for heavy number crunching. In particular, pyqtgraph uses Qt’s GraphicsView framework which is a highly capable graphics system on its own; we bring optimized and simplified primitives to this framework to allow data visualization with minimal effort.
It is known to run on Linux, Windows, and OSX
What can it do?
Amongst the core features of pyqtgraph are:
Basic data visualization primitives: Images, line and scatter plots
Fast enough for realtime update of video/plot data
Interactive scaling/panning, averaging, FFTs, SVG/PNG export
Widgets for marking/selecting plot regions
Widgets for marking/selecting image region-of-interest and automatically slicing multi-dimensional image data
Framework for building customized image region-of-interest widgets
Docking system that replaces/complements Qt’s dock system to allow more complex (and more predictable) docking arrangements
ParameterTree widget for rapid prototyping of dynamic interfaces (Similar to the property trees in Qt Designer and many other applications)
Examples
PyQtGraph includes an extensive set of examples that can be accessed by running either python -m pyqtgraph.examples or
import pyqtgraph.examples
pyqtgraph.examples.run()
This will start a launcher with a list of available examples. Select an item from the list to view its source code and double-click an item to run the example.
How does it compare to…
matplotlib: For plotting, pyqtgraph is not nearly as complete/mature as matplotlib, but runs much faster. Matplotlib is more aimed toward making publication-quality graphics, whereas pyqtgraph is intended for use in data acquisition and analysis applications. Matplotlib is more intuitive for matlab programmers; pyqtgraph is more intuitive for python/qt programmers. Matplotlib (to my knowledge) does not include many of pyqtgraph’s features such as image interaction, volumetric rendering, parameter trees, flowcharts, etc.
pyqwt5: About as fast as pyqtgraph, but not quite as complete for plotting functionality. Image handling in pyqtgraph is much more complete (again, no ROI widgets in qwt). Also, pyqtgraph is written in pure python, so it is more portable than pyqwt, which often lags behind pyqt in development (I originally used pyqwt, but decided it was too much trouble to rely on it as a dependency in my projects). Like matplotlib, pyqwt (to my knowledge) does not include many of pyqtgraph’s features such as image interaction, volumetric rendering, parameter trees, flowcharts, etc.
(My experience with these libraries is somewhat outdated; please correct me if I am wrong here)
使用pyqtgraph
How to use pyqtgraph
There are a few suggested ways to use pyqtgraph:
From the interactive shell (python -i, ipython, etc)
Displaying pop-up windows from an application
Embedding widgets in a PyQt application
Command-line use
PyQtGraph makes it very easy to visualize data from the command line. Observe:
import pyqtgraph as pg
pg.plot(data) # data can be a list of values or a numpy array
The example above would open a window displaying a line plot of the data given. The call to pg.plot returns a handle to the plot widget that is created, allowing more data to be added to the same window. Note: interactive plotting from the python prompt is only available with PyQt; PySide does not run the Qt event loop while the interactive prompt is running. If you wish to use pyqtgraph interactively with PySide, see the ‘console’ example.
Further examples:
pw = pg.plot(xVals, yVals, pen=‘r’) # plot x vs y in red
pw.plot(xVals, yVals2, pen=‘b’)
win = pg.GraphicsLayoutWidget() # Automatically generates grids with multiple items
win.addPlot(data1, row=0, col=0)
win.addPlot(data2, row=0, col=1)
win.addPlot(data3, row=1, col=0, colspan=2)
pg.show(imageData) # imageData must be a numpy array with 2 to 4 dimensions
We’re only scratching the surface here–these functions accept many different data formats and options for customizing the appearance of your data.
Displaying windows from within an application
While I consider this approach somewhat lazy, it is often the case that ‘lazy’ is indistinguishable from ‘highly efficient’. The approach here is simply to use the very same functions that would be used on the command line, but from within an existing application. I often use this when I simply want to get a immediate feedback about the state of data in my application without taking the time to build a user interface for it.
Embedding widgets inside PyQt applications
For the serious application developer, all of the functionality in pyqtgraph is available via widgets that can be embedded just like any other Qt widgets. Most importantly, see: PlotWidget, ImageView, GraphicsLayoutWidget, and GraphicsView. PyQtGraph’s widgets can be included in Designer’s ui files via the “Promote To…” functionality:
In Designer, create a QGraphicsView widget (“Graphics View” under the “Display Widgets” category).
Right-click on the QGraphicsView and select “Promote To…”.
Under “Promoted class name”, enter the class name you wish to use (“PlotWidget”, “GraphicsLayoutWidget”, etc).
Under “Header file”, enter “pyqtgraph”.
Click “Add”, then click “Promote”.
See the designer documentation for more information on promoting widgets. The “VideoSpeedTest” and “ScatterPlotSpeedTest” examples both demonstrate the use of .ui files that are compiled to .py modules using pyuic5 or pyside-uic. The “designerExample” example demonstrates dynamically generating python classes from .ui files (no pyuic5 / pyside-uic needed).
HiDPI Displays
PyQtGraph has a method mkQApp that by default sets what we have tested to be the best combination of options to support hidpi displays, when in combination with non-hidpi secondary displays. For your application, you may have instantiated QApplication yourself, in which case we advise setting these options before runing QApplication.exec_().
For Qt6 bindings, this functionally “just works” without having to set any attributes.
On Versions of Qt >= 5.14 and < 6; you can get ideal behavior with the following lines:
os.environ[“QT_ENABLE_HIGHDPI_SCALING”] = “1”
QApplication.setHighDpiScaleFactorRoundingPolicy(QtCore.Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
If you are on Qt >= 5.6 and < 5.14; you can get near ideal behavior with the following lines:
QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps)
With the later, ideal behavior was not achieved.
pyqtgraph.Qt.mkQApp(name=None)[source]
Creates new QApplication or returns current instance if existing.
Arguments:
name
(str) Application name, passed to Qt
PyQt and PySide
PyQtGraph supports two popular python wrappers for the Qt library: PyQt and PySide. Both packages provide nearly identical APIs and functionality, but for various reasons (discussed elsewhere) you may prefer to use one package or the other. When pyqtgraph is first imported, it automatically determines which library to use by making the fillowing checks:
If PyQt5 is already imported, use that
Else, if PySide2 is already imported, use that
Else, if PySide6 is already imported, use that
Else, if PyQt6 is already imported, use that
Else, attempt to import PyQt5, PySide2, PySide6, PyQt6, in that order.
If you have both libraries installed on your system and you wish to force pyqtgraph to use one or the other, simply make sure it is imported before pyqtgraph:
import PySide2 ## this will force pyqtgraph to use PySide2 instead of PyQt5
import pyqtgraph as pg
Embedding PyQtGraph as a sub-package of a larger project
When writing applications or python packages that make use of pyqtgraph, it is most common to install pyqtgraph system-wide (or within a virtualenv) and simply call import pyqtgraph from within your application. The main benefit to this is that pyqtgraph is configured independently of your application and thus you (or your users) are free to install newer versions of pyqtgraph without changing anything in your application. This is standard practice when developing with python.
Occasionally, a specific program needs to be kept in working order for an extended amount of time after development has been completed. This is often the case for single-purpose scientific applications. If we want to ensure that the software will still work ten years later, then it is preferable to tie it to a very specific version of pyqtgraph and avoid importing the system-installed version, which may be much newer and potentially incompatible. This is especially true when the application requires site-specific modifications to the pyqtgraph package.
To support such a separate local installation, all internal import statements in pyqtgraph are relative. That means that pyqtgraph never refers to itself internally as ‘pyqtgraph’. This allows the package to be renamed or used as a sub-package without any naming conflicts with other versions of pyqtgraph on the system.
The basic approach is to clone the repository into the appropriate location in your project. When you import pyqtgraph, be sure to use the full name to avoid importing any system-installed pyqtgraph packages. For example, imagine a simple project has the following structure:
my_project/
init.py
plotting.py
“”“Plotting functions used by this package”“”
import pyqtgraph as pg
def my_plot_function(*data):
pg.plot(*data)
To embed a specific version of pyqtgraph, we would clone the pyqtgraph repository inside the project, with a directory name that distinguishes it from a system-wide installation:
my_project$ git clone https://github.com/pyqtgraph/pyqtgraph.git local_pyqtgraph
Then adjust the import statements accordingly:
my_project/
init.py
local_pyqtgraph/
plotting.py
“”“Plotting functions used by this package”“”
import local_pyqtgraph.pyqtgraph as pg # be sure to use the local subpackage
# rather than any globally-installed
# version.
def my_plot_function(*data):
pg.plot(*data)
Use git checkout pyqtgraph-x.x.x to select a specific library version from the repository, or use git pull to pull pyqtgraph updates from upstream (see the git documentation for more information). If you do not plan to make use of git’s versioning features, adding the option --depth 1 to the git clone command retrieves only the latest version.
For projects that already use git for code control, it is also possible to include pyqtgraph as a git subtree within your own repository. The major advantage to this approach is that, in addition to being able to pull pyqtgraph updates from the upstream repository, it is also possible to commit your local pyqtgraph changes into the project repository and push those changes upstream:
my_project$ git remote add pyqtgraph https://github.com/pyqtgraph/pyqtgraph.git
my_project$ git fetch pyqtgraph
my_project$ git merge -s ours --allow-unrelated-histories --no-commit pyqtgraph/master
my_project$ mkdir local_pyqtgraph
my_project$ git read-tree -u --prefix=local_pyqtgraph/ pyqtgraph/master
my_project$ git commit -m “Added pyqtgraph to project repository”
See the git subtree documentation for more information.
转载:https://blog.csdn.net/weixin_45228198/article/details/128140662