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_())
相关推荐
笑鸿的学习笔记14 分钟前
qt-C++语法笔记之Stretch与Spacer的关系分析
c++·笔记·qt
大模型真好玩21 分钟前
准确率飙升!GraphRAG如何利用知识图谱提升RAG答案质量(额外篇)——大规模文本数据下GraphRAG实战
人工智能·python·mcp
198922 分钟前
【零基础学AI】第30讲:生成对抗网络(GAN)实战 - 手写数字生成
人工智能·python·深度学习·神经网络·机器学习·生成对抗网络·近邻算法
applebomb32 分钟前
没合适的组合wheel包,就自行编译flash_attn吧
python·ubuntu·attention·flash
Chasing__Dreams1 小时前
python--杂识--18.1--pandas数据插入sqlite并进行查询
python·sqlite·pandas
彭泽布衣2 小时前
python2.7/lib-dynload/_ssl.so: undefined symbol: sk_pop_free
python·sk_pop_free
喜欢吃豆2 小时前
从零构建MCP服务器:FastMCP实战指南
运维·服务器·人工智能·python·大模型·mcp
一个处女座的测试3 小时前
Python语言+pytest框架+allure报告+log日志+yaml文件+mysql断言实现接口自动化框架
python·mysql·pytest
nananaij3 小时前
【Python基础入门 re模块实现正则表达式操作】
开发语言·python·正则表达式
蛋仔聊测试3 小时前
Playwright 网络流量监控与修改指南
python