氣輕PyQt5 34 數(shù)據(jù)可視化(QChart, QSplineSeries)
?
QChart和 QChartView是數(shù)據(jù)可視化的控件,結(jié)合QSplineSeries制作了一條曲線。
注:數(shù)據(jù)來源:國家統(tǒng)計局
?
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtChart import QSplineSeries, QLineSeries, QChart, QChartView, QValueAxis, QBarCategoryAxis
import sys
?
class PyQt534(QMainWindow):
??? def __init__(self):
??????? super().__init__()
??????? self.initUI()
?
??? def initUI(self):
??????? self.setWindowTitle("chart")
??????? self.resize(600, 400)?????????????????????????? # 設(shè)置窗口大小
?
??????? gdpData? = [30319.98, 18809.64, 32679.87, 12820.4, 13509.15,
??????????????????? 12001.52, 22859.35, 24221.98, 20363.19, 15342.77]
?
??????? chart = QChart()??????????????????????????????? # 圖表組件
?
??????? spline = QSplineSeries()??????????????????????? # 樣條曲線
??????? for i, value in enumerate(gdpData):
??????????? spline.append(i, value)
?
?
??????? spline.setName("2018年部分城市GDP")
??????? spline.setColor(Qt.blue)
???????????????
??????? chart.addSeries(spline)???????????????????????? # 添加Series
?
??????? chartView = QChartView()??????????????????????? # 視圖組件
??????? chartView.setChart(chart)?????????????????????? # 顯示chart
???????
??????? self.setCentralWidget(chartView)
?
??????? self.show()
?
if __name__ == '__main__':
??? app = QApplication(sys.argv)
??? window = PyQt534()
??? sys.exit(app.exec_())
?
執(zhí)行結(jié)果
