目录
效果:
代码:
import sys
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5.QtCore import Qt
import pyqtgraph as pg
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
自定义横坐标
class RotateAxisItem(pg.AxisItem):
def drawPicture(self, p, axisSpec, tickSpecs, textSpecs):
p.setRenderHint(p.Antialiasing,False)
p.setRenderHint(p.TextAntialiasing,True)
## draw long line along axis
pen,p1,p2 = axisSpec
p.setPen(pen)
p.drawLine(p1,p2)
p.translate(0.5,0) ## resolves some damn pixel ambiguity
## draw ticks
for pen,p1,p2 in tickSpecs:
p.setPen(pen)
p.drawLine(p1,p2)
## draw all text
# if self.tickFont is not None:
# p.setFont(self.tickFont)
p.setPen(self.pen())
for rect,flags,text in textSpecs:
# this is the important part
p.save()
p.translate(rect.x(),rect.y())
p.rotate(-30)
p.drawText(-rect.width(),rect.height(),rect.width(),rect.height(),flags,text)
# restoring the painter is *required*!!!
p.restore()
直方图控件
class PyQtGraphHistWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.init_data()
self.init_ui()
def init_data(self):
self.color_bar = (107,200,224)
pass
def init_ui(self):
self.title_label = QtWidgets.QLabel('直方图')
self.title_label.setAlignment(Qt.AlignCenter)
xax = RotateAxisItem(orientation='bottom')
xax.setHeight(h=80)
self.pw = pg.PlotWidget(axisItems={'bottom': xax})
self.pw.setMouseEnabled(x=True, y=False)
# self.pw.enableAutoRange(x=False,y=True)
self.pw.setAutoVisible(x=False, y=True)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.title_label)
layout.addWidget(self.pw)
self.setLayout(layout)
pass
def set_data(self,data:Dict[str,Any]):
title_str = data['title_str']
x = data['x']
y = data['y']
y_name = data['y_name']
xTick = [data['xTick']]
self.y_datas = y
self.x_data = xTick
self.x_Tick = data['xTick']
self.y_name = y_name
self.title_label.setText(title_str)
self.pw.setLabel('left', y_name)
xax = self.pw.getAxis('bottom')
xax.setTicks(xTick)
barItem = pg.BarGraphItem(x=x,height=y,width=0.8,brush=self.color_bar)
self.pw.addItem(barItem)
self.vLine = pg.InfiniteLine(angle=90, movable=False)
self.hLine = pg.InfiniteLine(angle=0, movable=False)
self.label = pg.TextItem()
self.pw.addItem(self.vLine, ignoreBounds=True)
self.pw.addItem(self.hLine, ignoreBounds=True)
self.pw.addItem(self.label, ignoreBounds=True)
self.vb = self.pw.getViewBox()
self.proxy = pg.SignalProxy(self.pw.scene().sigMouseMoved, rateLimit=60, slot=self.mouseMoved)
self.pw.enableAutoRange()
pass
def mouseMoved(self,evt):
pos = evt[0]
if self.pw.sceneBoundingRect().contains(pos):
mousePoint = self.vb.mapSceneToView(pos)
index = int(mousePoint.x())
if index >= 0 and index < len(self.y_datas):
x_str = str(self.x_Tick[index][1])
y_str_html = ''
y_str = str(self.y_datas[index])
y_str_html += ' ' + y_str
html_str = '<p style="color:black;font-size:18px;font-weight:bold;"> ' + x_str +'<br/> '+y_str_html+ '</p>'
self.label.setHtml(html_str)
self.label.setPos(mousePoint.x(), mousePoint.y())
self.vLine.setPos(mousePoint.x())
self.hLine.setPos(mousePoint.y())
pass
pass
使用:
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
pre_data = {}
pre_data['title_str'] = '直方图'
pre_data['x'] = [0,1,2,3,4,5,6]
pre_data['y'] = [10,12,2,15,20,24,15]
pre_data['y_name'] = '测试'
pre_data['xTick'] = [(0,'1~10'),(1,'11~20'),(2,'21~30'),(3,'31~40'),(4,'41~50'),(5,'51~60'),(6,'61~70')]
t_win = PyQtGraphHistWidget()
t_win.show()
t_win.set_data(pre_data)
sys.exit(app.exec_())
pass