我一直在尝试使用 PyQtChart 构建图表。
我一直在关注这个实现,这就是我想要制作的。
https://doc.qt.io/qt-5/qtcharts-datetimeaxis-example.html
图表显示但缺少数据和 X 轴。
如何获取 X 轴上显示的日期时间和绘图上显示的数据?
from PyQt5.QtChart import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from datetime import datetime as dt
a = QApplication([])
#https://doc.qt.io/qt-5/qtcharts-datetimeaxis-example.html
x = ['2018-07-01 13:06:38', '2018-07-01 12:46:38', '2018-07-01 12:36:38', '2018-07-01 12:26:38', '2018-07-01 12:16:38', '2018-07-01 12:06:38', '2018-07-01 11:56:38', '2018-07-01 11:46:38', '2018-07-01 11:36:38', '2018-07-01 11:26:38', '2018-07-01 10:56:38', '2018-07-01 10:46:38', '2018-07-01 10:36:38']
y = [23.5, 20.8, 28.0, 28.1, 28.0, 27.8, 27.3, 27.2, 25.7, 24.7, 25.0, 25.0, 24.9]
#Chart Type
series = QLineSeries()
# Create Chart and set General Chart setting
chart = QChart()
chart.addSeries(series)
chart.setTitle("Temperature records in celcius")
chart.setAnimationOptions(QChart.SeriesAnimations)
# X Axis Settings
axisX = QDateTimeAxis()
axisX.setTickCount(10)
axisX.setFormat("dd HH mm") #https://doc.qt.io/qt-5/qdatetime.html#toString-2
axisX.setTitleText("Day")
chart.addAxis(axisX, Qt.AlignBottom)
series.attachAxis(axisX)
# Y Axis Settings
axisY = QValueAxis()
axisY.setLabelFormat("%i")
axisY.setTitleText("Temperature C")
chart.addAxis(axisY, Qt.AlignLeft)
series.attachAxis(axisY)
# Create a QChartView object with QChart as a parameter. This way we don't need to create the QGraphicsView scene ourselves. We also set the Antialiasing on to have the rendered lines look nicer.
chartView = QChartView(chart)
chartView.setRenderHint(QPainter.Antialiasing)
chart.createDefaultAxes()
chart.axisY(series).setRange(min(y)-5, max(y)+5)
chart.legend().setVisible(True)
chart.legend().setAlignment(Qt.AlignBottom)
chartView = QChartView(chart)
chartView.setRenderHint(QPainter.Antialiasing)
window = QMainWindow()
window.setCentralWidget(chartView)
window.resize(600, 300)
window.show()
a.exec_()