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_())
相关推荐
java1234_小锋16 小时前
TensorFlow2 Python深度学习 - 循环神经网络(RNN)- 简介
python·深度学习·tensorflow·tensorflow2
大飞记Python16 小时前
Chromedriver放项目里就行!Selenium 3 和 4 指定路径方法对比 + 兼容写法
开发语言·python
小薛引路16 小时前
office便捷办公06:根据相似度去掉excel中的重复行
windows·python·excel
Hs_QY_FX16 小时前
Python 分类模型评估:从理论到实战(以信用卡欺诈检测为例)
人工智能·python·机器学习·数据挖掘·多分类评估
Gitpchy16 小时前
Day 18 推断聚类后簇的类型
python·机器学习·聚类
黑马金牌编程17 小时前
Jenkins的Linux与window部署方式
linux·运维·windows·jenkins·持续集成·cicd
jjjxxxhhh12317 小时前
【项目-】Qt + QCustomPlot 实现频谱监测仪:四图联动、高频信号注入、鼠标交互全解析
开发语言·qt·交互
查士丁尼·绵17 小时前
笔试-基站维护
python
deephub17 小时前
REFRAG技术详解:如何通过压缩让RAG处理速度提升30倍
人工智能·python·大语言模型·rag
唐叔在学习17 小时前
venv - Python最佳的轻量化环境隔离方式
后端·python