使用方法,上面輸入框內修改數字後按回車就更新圖表
import sys
from PyQt5.QtWidgets import (QWidget, QLineEdit, QGridLayout,QLabel, QApplication)
from PyQt5.QtGui import QPen,QColor,QBrush,QLinearGradient
import pyqtgraph as pg
# 前景(座標軸,網格)消除鋸齒
pg.setConfigOptions(foreground=QColor(113,148,116), antialias=True)
class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
self.edit = QLineEdit('11', self)
self.edit.editingFinished.connect(self.Func_1)
# 創建圖
self.myplot = pg.PlotWidget()
self.myplot.enableAutoRange()
layout = QGridLayout(self)
layout.addWidget(self.edit, 0, 0)
layout.addWidget(self.myplot, 1, 0, 3, 3)
self.myplot.setBackground((210, 240, 240)) # 背景色
self.myplot.showGrid(y=True)
# 初始點
self.points = [1, 2, 3, 4, 5, 6,1, 12, 23, 12, -11]
self.update()
def Func_1(self):
self.points.append(int(self.edit.text()))
self.update()
def update(self):
pen = pg.mkPen({'color': (155,200,160), 'width': 4}) # 畫筆設置
self.myplot.plot(self.points[-100:], clear=True, pen=pen,symbol='o',symbolBrush=QColor(113,148,116)) # symbol:折點樣式,symbolBrush:折點顏色
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
運行結果:
但是呢!比較討厭的就是鼠標事件,很醜還不好改,打算禁用掉,官方文檔並沒有給出禁用的接口
好奇寶寶入口:
http://www.pyqtgraph.org/documentation/mouse_interaction.html
http://www.pyqtgraph.org/documentation/graphicsscene/mouseclickevent.html
我的解決辦法:寫個子類,重寫鼠標事件方法
class pgg(pg.PlotWidget,):
def __init__(self):
super(pgg,self).__init__()
#self.setupUi(self)
#super(pg.PlotWidget, self).__init__(self)
def mouseMoveEvent(self, ev):
print("略略略")
def mouseReleaseEvent(self, ev):
print("開心")
def mousePressEvent(self,ev):
print("祕密")
#pg.PlotWidget.mousePressEvent(self,ev)
def EnableAutoRange(self):
pg.PlotWidget.mouseEnabled = False
其他功能見:
from pyqtgraph import examples
examples.run()