QChart和QChartView是数据可视化的控件,结合QSplineSeries制作了一条曲线。
注:数据来源:国家统计局
【资料图】
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) # 设置窗口大小
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_())
执行结果
标签: