使用方法,上面输入框内修改数字后按回车就更新图表
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()