python pyqt繪制直方圖
阿新 • • 發佈:2017-12-04
borde histogram etc width mys black pac cross def
# -*- coding: utf-8 -*- """ In this example we draw two different kinds of histogram. """ from qtpy import QtWidgets, QtGui, QtCore from qtpy.QtWidgets import QApplication, QWidget import datetime as dt from vnpy.trader import * from vnpy.trader.uiKLine import * from vnpy.trader.widget.crosshairTool importCrosshairTool import pyqtgraph as pg import numpy as np import matplotlib.pyplot as plt import matplotlib.mlab as mlab import sys class HistogramLUTWidget(QWidget): def __init__(self, data, parent=None): self.parent = parent self.data = data super(HistogramLUTWidget, self).__init__(parent) # 繼承 # 界面布局 self.pw=pg.PlotWidget() self.lay_KL = pg.GraphicsLayout(border=(100, 100, 100)) self.lay_KL.setContentsMargins(10, 10, 10, 10) self.lay_KL.setSpacing(0) self.lay_KL.setBorder(color=(255, 255, 255, 255), width=0.8) self.lay_KL.setZValue(0) self.pw.setCentralItem(self.lay_KL)# # 設置橫坐標 # xdict = dict(enumerate(self.data["timeList"])) # self.axisTime = MyStringAxis(xdict, orientation=‘bottom‘) # # 初始化子圖 self.initplotpnl() # # 註冊十字光標 x=[] viwe = self.plt self.crosshair = CrosshairTool(self.pw, x, viwe, self) # 設置界面 self.vb = QtWidgets.QVBoxLayout() self.vb.addWidget(self.pw) self.setLayout(self.vb) # ---------------------------------------------------------------------- def initplotpnl(self, xmin=0, xmax=-1): vals = np.hstack(self.data) xMin = min(self.data) xMax = max(self.data) #histogram #y, x = np.histogram(vals, bins=np.linspace(xMin,xMax, 100),normed=False,density=True) # #hist nx, xbins, ptchs = plt.hist(self.data, bins=50, normed=True, facecolor=‘black‘, edgecolor=‘black‘,alpha=1,histtype = ‘bar‘) histo = plt.hist(self.data, 50) vb = CustomViewBox() # self.plt=pg.PlotItem(viewBox=vb, axisItems={‘bottom‘: self.axisTime})#設置x軸 self.plt = pg.PlotItem(viewBox=vb) leng_ = len(self.data) self.plt.setRange(xRange=(0, leng_), yRange=(xMin, xMax)) # #histogram # self.plt.plot(x, y, stepMode=True, fillLevel=0, brush=(0, 0, 255, 50),size=0.4)#繪制統計所得到的概率密度,直方圖,沒有邊界分割線 ##hist bar width = xbins[1] - xbins[0] # Width of each bin. #self.plt.plot(x,y,width=width)# 繪制統計所得到的概率密度,線形圖 self.pi=pg.BarGraphItem(x=histo[1][0:50],height=histo[0],width=width, color=‘y‘,)# 繪制統計所得到的概率密度,bar圖 self.plt.addItem(self.pi) self.lay_KL.addItem(self.plt) def refresh(self): """ 刷新子圖的現實範圍 """ leng_ = len(self.data) xMin = min(self.data) xMax = max(self.data) self.plt.setRange(xRange=(0, leng_), yRange=(xMin, xMax)) if __name__ == ‘__main__‘: app = QApplication(sys.argv) ex = HistogramLUTWidget() sys.exit(app.exec_())
####才接觸,項目急沒有細研究,有錯誤的改正的地方,望大家不吝指出
python pyqt繪制直方圖