本文主要是介绍【PYQT5】pyqtgraph 绘制图表 样式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
使用方法,上面输入框内修改数字后按回车就更新图表
import sys
from PyQt5.QtWidgets import (QWidget, QLineEdit, QGridLayout,QLabel, QApplication)
from PyQt5.QtGui import QPen,QColor,QBrush,QLinearGradientimport 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()
这篇关于【PYQT5】pyqtgraph 绘制图表 样式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!