python pyqt绘制直方图

Posted Pavilion

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python pyqt绘制直方图相关的知识,希望对你有一定的参考价值。

# -*- 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 import  CrosshairTool
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绘制直方图的主要内容,如果未能解决你的问题,请参考以下文章

Python绘制概率分布直方图

Python Seaborn 绘制空白直方图

Python绘制图像的灰度直方图累计直方图

学习 PyQt5。在我的代码片段中找不到错误 [关闭]

python 计算并绘制灰度直方图

使用 csv 文件作为输入在 python 中绘制直方图