windows python qt5 QChartView画折线图

环境:windows pyqt5 ,用QCartView画折线图

环境需要提前安装 pip install PyQtChart

折线图随着时间推移会不断移动,主动更新x轴坐标

python 复制代码
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout
from PyQt5.QtChart import QChart, QChartView ,QDateTimeAxis ,QValueAxis ,QSplineSeries
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import QDateTime ,QTimer
import numpy as np

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QChart Demo示例绘图")
        self.initUI()
        self.resize(600, 500)

    def initUI(self):
        layout = QVBoxLayout()
        self.setLayout(layout)

        self.series1 = QSplineSeries()
        self.series1.setName("series1")
        self.series2 = QSplineSeries()
        self.series2.setName("series2")

        self.chart = QChart()
        self.chart.setTitle("测试图")
        self.chart.setTheme(QChart.ChartTheme.ChartThemeDark)

        self.chart_view = QChartView()
        self.chart_view.setChart(self.chart)
        self.chart_view.setRenderHint(QPainter.Antialiasing)
        layout.addWidget(self.chart_view)

        self.chart.addSeries(self.series1)
        self.chart.addSeries(self.series2)
        self.axisXTime = QDateTimeAxis()
        self.axisXTime.setFormat("hh:mm:ss")
        self.axisXTime.setTickCount(10)
        self.axisXTime.setTitleText("time")
        self.axisXTime.setRange(QDateTime.currentDateTime(), QDateTime.currentDateTime().addSecs(30*2))

        self.axisY = QValueAxis()
        self.axisY.setTickCount(5)
        # axisY.setLabelFormat("%.2f")
        self.axisY.setTitleText("value")
        self.axisY.setRange(0, 100)

        self.chart.setAxisX(self.axisXTime, self.series1)
        self.chart.setAxisY(self.axisY, self.series1)

        self.chart.setAxisX(self.axisXTime, self.series2)
        self.chart.setAxisY(self.axisY, self.series2)

        # 创建定时器
        self.timer = QTimer()
        self.timer.timeout.connect(self.update_view)
        # 设置定时器间隔为1000毫秒(1秒)
        self.timer.start(1000)

    def update_view(self):
        now_time = QDateTime.currentDateTime()
        now_time_np = np.int64(now_time.toMSecsSinceEpoch())

        self.series1.append(now_time_np , np.random.rand()*100)
        self.series2.append(now_time_np , np.random.rand()*100)

        if now_time_np > self.axisXTime.max().toMSecsSinceEpoch():
            self.axisXTime.setRange(now_time.addSecs(-30), now_time.addSecs(30))

        y_data = np.random.rand()
        if y_data > self.axisY.max() or self.axisY.min() > y_data :
            self.axisY.setMax(max(y_data + 10, self.axisY.max()));
            self.axisY.setMin(min(y_data - 10, self.axisY.min()));


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
相关推荐
Allen_LVyingbo几秒前
面向70B多模态医疗大模型预训练的工程落地(医疗大模型预训练扩展包)
人工智能·python·分类·知识图谱·健康医疗·迁移学习
Deng872347348几秒前
电脑使用 Gemini出了点问题解决办法
人工智能·python
我送炭你添花5 分钟前
Pelco KBD300A 模拟器:18. 按依赖顺序 + 复杂度由低到高逐步推进pytest单元测试
python·单元测试·log4j·pytest
程序员杰哥8 分钟前
如何写出高效的测试用例?
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
2301_822377658 分钟前
数据分析师的Python工具箱
jvm·数据库·python
凯子坚持 c12 分钟前
Qt常用控件指南(6)
开发语言·qt
少控科技15 分钟前
QT第三个程序 - 表达式计算器
开发语言·qt
轩情吖16 分钟前
Qt容器类控件之QGroupBox与QTabWidget
开发语言·c++·qt·qgroupbox·qtabwidget·桌面级开发
无垠的广袤17 分钟前
【VisionFive 2 Lite 单板计算机】SoC 温度的 Home Assistant 物联网终端显示
linux·python·物联网
强化试剂瓶17 分钟前
全面掌握Ergosterol-PEG-Biotin,麦角甾醇PEG生物素的使用与注意事项
python·scrapy·flask·scikit-learn·pyqt