直方图python_python pyqt绘制直方图

#-*- coding: utf-8 -*-

"""In this example we draw two different kinds of histogram."""

from qtpy importQtWidgets, QtGui, QtCorefrom qtpy.QtWidgets importQApplication, QWidgetimportdatetime as dtfrom vnpy.trader import *

from vnpy.trader.uiKLine import *

from vnpy.trader.widget.crosshairTool importCrosshairToolimportpyqtgraph as pgimportnumpy as npimportmatplotlib.pyplot as pltimportmatplotlib.mlab as mlabimportsysclassHistogramLUTWidget(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)defrefresh(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_())

####才接触,项目急没有细研究,有错误的改正的地方,望大家不吝指出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值