效果图显示的是 2025Q1 申万行业1,各行业的总资产柱状图
# -*- coding: utf-8-*-
import numpy as np
from PyQt6.QtGui import (
QColor
)
from PyQt6.QtWidgets import (
QApplication
)
import pyqtgraph as pg
class GraphHorizonalBarWidget(pg.PlotWidget):
def __init__(self):
super().__init__()
self.init_data()
def init_data(self):
self.color_blue = QColor(0, 0, 255)
self.color_red = QColor(255, 0, 0)
pass
def set_data(self, y_labels: list,x_values:list,light_y:int):
self.clear()
brush_list = [self.color_blue]*light_y + [self.color_red] + [self.color_blue]*(len(y_labels)-1-light_y)
y_pos = np.arange(len(y_labels))
bar = pg.BarGraphItem(x0=[0]*len(x_values),y=y_pos,height=0.8,width=x_values,brushes=brush_list)
self.addItem(bar)
for i,(y,x) in enumerate(zip(y_pos,x_values)):
text = pg.TextItem(
text=trans_float_to_money(x),
color='#FFFF00',
anchor=(0,0.5)
)
text.setPos(x,y)
self.addItem(text)
pass
self.getAxis('left').setTicks([[(i, label) for i, label in enumerate(y_labels)]])
self.setYRange(-0.5, len(y_labels) - 0.5)
pass
pass
def trans_float_to_money(val):
try:
val = float(val)
if abs(val) // 1000000000000 >= 1:
return f"{val / 1000000000000:,.2f}万亿"
elif abs(val) // 100000000 >= 1:
return f"{val / 100000000:,.2f}亿"
elif abs(val) // 10000 >= 1:
return f"{val / 10000:,.2f}万"
else:
return f"{val:,.2f}"
except:
return f"{val}"
def testGraphHorizonalBarWidget():
y_labels = ['银行', '非银金融', '建筑装饰', '房地产', '公用事业', '化工', '采掘', '交通运输', '电气设备', '电子', '汽车', '通信', '机械设备', '医药生物', '有色金属', '钢铁', '家用电器', '食品饮料', '计算机', '商业贸易', '建筑材料', '国防军工', '农林牧渔', '轻工制造', '纺织服装', '综合', '休闲服务']
x_values = [314024668995698.1, 43048344756485.4, 15949706560967.38, 10917897410050.17, 9371233950175.68, 8115235330558.25, 7294203379434.87, 6684140641094.06, 5660779292266.4, 5572346783395.04, 5017953366539.23, 4610419991898.09, 4567821116008.13, 4026838001386.43, 3383697438895.98, 2223668575778.81, 2030807753588.63, 1787231250490.51, 1736929806714.4, 1574472543254.69, 1529143874890.84, 1473613513857.0, 1337332888569.07, 1083847573396.27, 448486583841.67, 333493417429.92, 265001853890.36]
h_y = 1
app = QApplication([])
mv = GraphHorizonalBarWidget()
mv.set_data(y_labels[1:],x_values[1:],h_y)
mv.showMaximized()
app.exec()
pass
if __name__ == '__main__':
testGraphHorizonalBarWidget()
pass