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_())
相关推荐
柳鲲鹏4 分钟前
WINDOWS最快布署WEB服务器:apache2
服务器·前端·windows
思则变2 小时前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest
漫谈网络3 小时前
WebSocket 在前后端的完整使用流程
javascript·python·websocket
专注VB编程开发20年3 小时前
开机自动后台运行,在Windows服务中托管ASP.NET Core
windows·后端·asp.net
try2find4 小时前
安装llama-cpp-python踩坑记
开发语言·python·llama
博观而约取5 小时前
Django ORM 1. 创建模型(Model)
数据库·python·django
李洋-蛟龙腾飞公司5 小时前
HarmonyOS NEXT应用元服务常见列表操作分组吸顶场景
linux·运维·windows
码农垦荒笔记6 小时前
Git 安装闭坑指南(仅 Windows 环境)
windows·git
精灵vector6 小时前
构建专家级SQL Agent交互
python·aigc·ai编程
Zonda要好好学习7 小时前
Python入门Day2
开发语言·python