赞
踩
QBarSeries创建的垂直方向绘制的柱状图。创建在水平方向绘制的柱状图则需要使用类QHorizontalBarSeries来实现。二者的各种原理是一样的,只是后者在水平方向绘制。
QHorizontalBarSeries类将一系列数据显示为按类别分组的水平柱状条。
示例显示了如何创建水平柱状图,完整代码如下:
- import sys
- from PyQt5.QtGui import QPainter, QPen
- from PyQt5.QtCore import Qt
- from PyQt5.QtWidgets import QApplication, QMainWindow
- from PyQt5.QtChart import (QChartView, QChart, QHorizontalBarSeries, QBarSet,
- QLegend, QBarCategoryAxis, QValueAxis)
-
- class DemoHorizontalBarSeries(QMainWindow):
- def __init__(self, parent=None):
- super(DemoHorizontalBarSeries, self).__init__(parent)
-
- # 设置窗口标题
- self.setWindowTitle('实战 Qt for Python: 水平柱状图演示')
- # 设置窗口大小
- self.resize(480, 360)
-
- self.createChart()
-
- def createChart(self):
-
- #创建条状单元
- barSet0 = QBarSet('Jane')
- barSet1 = QBarSet('Jone')
- barSet2 = QBarSet('Axel')
- barSet3 = QBarSet('Mary')
- barSet4 = QBarSet('Samantha')
-
- barSet0.append([1, 2, 3, 4, 5, 6])
- barSet1.append([5, 0, 0, 4, 0, 7])
- barSet2.append([3, 5, 8, 13, 8, 5])
- barSet3.append([5, 6, 7, 3, 4, 5])
- barSet4.append([9, 7, 5, 3, 1, 2])
-
- #条状图
- barSeries = QHorizontalBarSeries()
- barSeries.append(barSet0)
- barSeries.append(barSet1)
- barSeries.append(barSet2)
- barSeries.append(barSet3)
- barSeries.append(barSet4)
-
- #创建图表
- chart = QChart()
- chart.addSeries(barSeries)
- chart.setTitle('简单水平柱状图示例')
- chart.setAnimationOptions(QChart.SeriesAnimations) #设置成动画显示
-
- #设置横向坐标(X轴)
- categories = ['一月', '二月', '三月', '四月', '五月', '六月']
- axisY = QBarCategoryAxis()
- axisY.append(categories)
- chart.addAxis(axisY, Qt.AlignLeft)
- barSeries.attachAxis(axisY)
-
- #设置纵向坐标(Y轴)
- axisX = QValueAxis()
- axisX.setRange(0, 15)
- axisX.setTickCount(4)
- chart.addAxis(axisX, Qt.AlignBottom)
- barSeries.attachAxis(axisX)
-
- #图例属性
- chart.legend().setVisible(True)
- chart.legend().setAlignment(Qt.AlignBottom)
-
- #图表视图
- chartView = QChartView(chart)
- chartView.setRenderHint(QPainter.Antialiasing)
-
- self.setCentralWidget(chartView)
-
-
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- window = DemoHorizontalBarSeries()
- window.show()
- sys.exit(app.exec())
运行结果如下图:
QChart水平柱状图示例
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。