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_())
相关推荐
kobe_OKOK_6 小时前
__getattr__和__getattribute__如何用
python
全栈弄潮儿7 小时前
Python实战第2期: 变量与数据类型
后端·python·agent
长沙三为智能科技7 小时前
家政小程序门店运营模块设计:会员、下单、派单、结算四段链路的落地拆解
python·小程序·内容运营
Cc.Y7 小时前
Java零基础入门:可变字符串与包装类:StringBuilder、StringBuffer 与 通讯录管理系统实战
java·开发语言·python
启观川8 小时前
数据结构与算法 -第 3 章 常用算法-动态规划
数据结构·笔记·python·算法
码爸8 小时前
排序算法介绍
python·算法·排序算法
笔墨登场说说8 小时前
flink bin/start-cluster.sh 帮我做成开机启动
开发语言·python
bug和崩溃我都要8 小时前
QT 开发的专业截图录屏工具 - macOS版 / Windows版
windows·qt·mac·录屏·截屏
kimloner8 小时前
开源一个 376KB 的 Windows 桌面小工具:便签 + 截图标注 + OCR + 待办提醒(C# / WinForms)
windows·开源·c#·ocr·winforms
摸鱼 特供版9 小时前
一款边录边标注的轻量截图录屏工具
windows·学习·电脑·音视频·软件需求