PyQtGraph 繪圖
Python語言 的數據可視化(繪圖) 方法,常見的有 Matplotlib 和 PyQtGraph
- Matplotlib
說到 Python語言 的數據作圖, Matplotlib 當然是最有名的。
優點: 功能完備、成熟穩定、社區生態圈龐大。
缺點: 某些作圖場景性能不高,和Qt圖形界面框架 融合的不太好。
- PyQtGraph
PyQtGraph 是基於Qt 的純Python 庫。
優點: 大數據量的作圖性能遠高於 Matplotlib, 動態更新圖的性能也比Matplotlib高。
並且和Qt圖形界面框架完美融合,因為它就是基於Qt開發的。
缺點: 作圖功能沒有Matplotlib多,開發社區沒有Matplotlib大。
那么,我們應該使用哪種方案呢?我的建議是:
如果你已經使用Qt開發圖形界面程序了,並且作圖功能是PyQtGraph支持的, 建議使用 PyQtGraph,因為它和Qt界面無縫結合。
否則 使用 Matplotlib。
本文先介紹 PyQtGraph 的使用示例。
PyQtGraph 安裝
pip install pyqtgraph
官方案例
PyQtGraph 官方文檔包含了很多示例代碼,我們只需運行如下代碼,即可查看這些demo和對應的代碼
import pyqtgraph.examples pyqtgraph.examples.run()
XY 軸曲線圖 demo
嵌入到Qt程序界面中
from PySide2 import QtWidgets import pyqtgraph as pg class MainWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('pyqtgraph作圖示例') # 創建 PlotWidget 對象 self.pw = pg.PlotWidget() # 設置圖表標題 self.pw.setTitle("氣溫趨勢", color='008080', size='12pt') # 設置上下左右的label self.pw.setLabel("left","氣溫(攝氏度)") self.pw.setLabel("bottom","時間") # 設置Y軸 刻度 范圍 self.pw.setYRange(min=-10, # 最小值 max=50) # 最大值 # 顯示表格線 self.pw.showGrid(x=True, y=True) # 背景色改為白色 self.pw.setBackground('w') # 居中顯示 PlotWidget self.setCentralWidget(self.pw) hour = [1,2,3,4,5,6,7,8,9,10] temperature = [30,32,34,32,33,31,29,32,35,45] # hour 和 temperature 分別是 : x, y 軸上的值 self.pw.plot(hour, temperature, pen=pg.mkPen('b') # 線條顏色 ) if __name__ == '__main__': app = QtWidgets.QApplication() main = MainWindow() main.show() app.exec_()
實時更新圖
要畫動態的實時更新圖,只需要在把變更的內容重新plot即可。
示例代碼如下
from PySide2 import QtWidgets from pyqtgraph.Qt import QtCore import pyqtgraph as pg import sys from random import randint class MainWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('pyqtgraph作圖') # 創建 PlotWidget 對象 self.pw = pg.PlotWidget() # 設置圖表標題 self.pw.setTitle("氣溫趨勢", color='008080', size='12pt') # 設置上下左右的label self.pw.setLabel("left","氣溫(攝氏度)") self.pw.setLabel("bottom","時間") # 設置Y軸 刻度 范圍 self.pw.setYRange(min=-10, # 最小值 max=50) # 最大值 # 顯示表格線 self.pw.showGrid(x=True, y=True) # 背景色改為白色 self.pw.setBackground('w') # 設置Y軸 刻度 范圍 self.pw.setYRange(min=-10, # 最小值 max=50) # 最大值 # 居中顯示 PlotWidget self.setCentralWidget(self.pw) # 實時顯示應該獲取 plotItem, 調用setData, # 這樣只重新plot該曲線,性能更高 self.curve = self.pw.getPlotItem().plot( pen=pg.mkPen('r', width=1) ) self.i = 0 self.x = [] # x軸的值 self.y = [] # y軸的值 # 啟動定時器,每隔1秒通知刷新一次數據 self.timer = QtCore.QTimer() self.timer.timeout.connect(self.updateData) self.timer.start(1000) def updateData(self): self.i += 1 self.x.append(self.i) # 創建隨機溫度值 self.y.append(randint(10,30)) # plot data: x, y values self.curve.setData(self.x,self.y) if __name__ == '__main__': app = QtWidgets.QApplication() main = MainWindow() main.show() app.exec_()
軸刻度為字符串
上面的程序運行起來, X軸的刻度是 數字, 如果我們希望軸刻度是文字怎么做呢?
我們參考了這個網址的介紹: https://stackoverflow.com/questions/31775468/show-string-values-on-x-axis-in-pyqtgraph?lq=1
需要定義從數字到字符串的映射列表,參考如下代碼
import pyqtgraph as pg # 刻度表,注意是雙層列表 xTick = [[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f')]] x = [0,1,2,3,4,5] y = [1, 2, 3, 4, 5, 6] win = pg.GraphicsWindow() stringaxis = pg.AxisItem(orientation='bottom') stringaxis.setTicks(xTick) plot = win.addPlot(axisItems={'bottom': stringaxis}) curve = plot.plot(x,y) pg.QtGui.QApplication.exec_()
獲取鼠標所在處刻度值
有時候,我們的程序需要獲取 鼠標在 pyqtgraph 圖形上移動時,鼠標所在對應的數據是什么。
可以參考下面代碼的作法
import pyqtgraph as pg import PySide2 x = [1,2,3,4,5] y = [1,2,3,4,5] win = pg.GraphicsWindow() plot = win.addPlot() curve = plot.plot(x,y) def mouseover(pos): act_pos = curve.mapFromScene(pos) if type(act_pos) != PySide2.QtCore.QPointF: return print(act_pos.x(),act_pos.y()) # 有了 act_pos的坐標值,就可以根據該值處理一些信息 # 比如狀態欄展示 該處的 日期等 curve.scene().sigMouseMoved.connect(mouseover) pg.QtGui.QApplication.exec_()