氣輕PyQt5 35 QChart坐標(biāo)軸(QValueAxis)
?
在【氣輕PyQt5 34】中制作了一條曲線,但縱坐標(biāo)的數(shù)據(jù)顯示似乎有些問題。為了改善顯示效果對縱坐標(biāo)進(jìn)行了QValueAxis設(shè)置。axis_y.setLabelFormat("%d")中的"%d"有許多變化,與c語言中的函數(shù)fprintf的格式類似,比如%4.2f等等。
?
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtChart import QSplineSeries, QLineSeries, QChart, QChartView, QValueAxis, QBarCategoryAxis
from math import ceil
import sys
?
class PyQt535(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]
??????? MaxGDP = ceil(max(gdpData)/9)*10
?
??????? chart = QChart()??????????????????????????????? # 圖表組件
??????? chart.setTitle("GDP")
?
??????? spline = QSplineSeries()????????????????? ??????# 樣條曲線
??????? for i, value in enumerate(gdpData):
??????????? spline.append(i, value)
?
??????? spline.setName("2018年部分城市GDP")
??????? spline.setColor(Qt.blue)
???????????????
??????? chart.addSeries(spline)???????????????????????? # 添加Series
?
???? ???axis_y = QValueAxis()
??????? axis_y.setLabelFormat("%d")
??????? axis_y.setRange(0, MaxGDP)
??????? axis_y.setTitleText("億元")
??????? chart.addAxis(axis_y, Qt.AlignLeft)
??????? spline.attachAxis(axis_y)
?
??????? chartView = QChartView()?????????????? ?????????# 視圖組件
??????? chartView.setChart(chart)?????????????????????? # 顯示chart
???????
??????? self.setCentralWidget(chartView)
?
??????? self.show()
?
if __name__ == '__main__':
??? app = QApplication(sys.argv)
??? window = PyQt535()
??? sys.exit(app.exec())
?
執(zhí)行結(jié)果
