RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1307278
Accepted
g_0008
g_0008
Asked:2022-07-21 11:45:40 +0000 UTC2022-07-21 11:45:40 +0000 UTC 2022-07-21 11:45:40 +0000 UTC

如何停止 FuncAnimation

  • 772

我有一个实时图表绘制添加到文件中的值。但是有这样一个时刻:当新数据停止进入文件时,动画就必须停止。FuncAnimation例如,如果字符串的长度没有改变,你怎么能停止提供的工作?像这样尝试:

if len(x)>m:
    m = len(x)
else:
    #остановить анимацию, но функции для остановки анимации не работают

提前感谢您的任何建议!

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.animation import FuncAnimation
import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT
import mplcursors
plt.style.use('seaborn-whitegrid')

def animation(i, axs):
    data = pd.read_csv('FlyData.txt', sep='\s+', encoding='latin-1', header=None)
    data = pd.DataFrame(data)
    colors = ['#FFD700', 'g', 'b', 'r', 'purple', 'c', 'm', 'y', 'k', 'c', '#FF4500', '#2F4F4F', '#FF69B4', '#556B2F', '#20B2AA', '#BDB76B', '#FFD700', '#DB7093', '#7FFF00', '#4B0082']
    labels = ['hr', 'Vпр, м\с', 'c', 'gh', 'h', 'j', 'h']
    x = data[0]
    m = 0
    for i in range(0, 7):
        y = data[i+1]
        ymax = max(y)
        ymin = min(y)
        stepx = max(x)/10
        axs[i].clear()
        axs[i].label_outer()
        axs[i].set_ylim([ymin*1.1, ymax*1.1])
        axs[i].set_xticks(np.arange(0, max(x)+1, stepx))
        axs[i].spines['right'].set_visible(False)
        axs[i].spines['top'].set_visible(False)
        axs[i].autoscale_view()
        axs[i].set_ylabel(labels[i])
        axs[i].plot(x, y, colors[i])

        if ymax > 1000 and ymax>2000:
            axs[i].set_yticks(np.arange(ymin, ymax, 400))
        elif ymax < 1000 and ymax>500:
            axs[i].set_yticks(np.arange(ymin, ymax, 200))
        elif ymax>100 and ymax<500:
            axs[i].set_yticks(np.arange(ymin, ymax, 50))
        elif ymax<100 and ymax>15:
            axs[i].set_yticks(np.arange(ymin, ymax, 20))
        elif ymax<15 and ymax>1:
            axs[i].set_yticks(np.arange(ymin, ymax, 2))
        else:
            axs[i].set_yticks(np.arange(ymin, ymax, 0.20))

        #DATA_POINTS_TO_DISPLAY = 200
        #data1 = deque(maxlen=DATA_POINTS_TO_DISPLAY)

    plt.xlabel('Время')
    mplcursors.cursor(hover=True)


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1119, 821)

        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")

        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)

        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setObjectName("pushButton_2")
        self.gridLayout.addWidget(self.pushButton_2, 0, 1, 1, 1)

        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setObjectName("pushButton_3")
        self.gridLayout.addWidget(self.pushButton_3, 0, 2, 1, 1)

        self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_4.setObjectName("pushButton_4")
        self.gridLayout.addWidget(self.pushButton_4, 0, 3, 1, 1)

        self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_5.setObjectName("pushButton_5")
        self.gridLayout.addWidget(self.pushButton_5, 0, 4, 1, 1)

        self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_6.setObjectName("pushButton_6")
        self.gridLayout.addWidget(self.pushButton_6, 0, 5, 1, 1)

        self.figure = plt.figure(dpi=100)
        self.gs = self.figure.add_gridspec(7, hspace=0)
        self.axs = self.gs.subplots(sharex=True, sharey=False)
        self.figure.tight_layout()


        #self.figure.canvas.mpl_connect('button_press_event', onClick)
        #anim_running = True
        self.ani = FuncAnimation(self.figure, animation, fargs = (self.axs,), interval = 1000, blit = False)
        self.canvas = FigureCanvasQTAgg(self.figure)
        self.toolbar = NavigationToolbar2QT(self.canvas, self.canvas)
        self.gridLayout.addWidget(self.canvas, 1, 0, 1, 18)
        self.canvas.draw()


        #self.widget = QtWidgets.QWidget(self.centralwidget)
        #self.widget.setObjectName("widget")
        #self.gridLayout.addWidget(self.widget, 1, 0, 1, 6)

        self.step = 1
        #self.scroll = QtWidgets.QScrollArea(self.centralwidget)
        #self.scroll.setWidget(self.canvas)
        #self.scroll.setWidgetResizable(True)
        #self.gridLayout.addWidget(self.scroll,  2, 0, 1, 18)
        self.scroll = QtWidgets.QScrollBar(self.centralwidget)
        self.scroll.setOrientation(QtCore.Qt.Horizontal)
        self.scroll.setPageStep(self.step * 100)
        self.scroll.setObjectName("scroll")
        self.gridLayout.addWidget(self.scroll,  2, 0, 1, 18)
        self.setupSlider()
        #self.scroll.sliderMoved.connect(self.canvas)

        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))
        self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
        self.pushButton_3.setText(_translate("MainWindow", "PushButton"))
        self.pushButton_4.setText(_translate("MainWindow", "PushButton"))
        self.pushButton_5.setText(_translate("MainWindow", "PushButton"))
        self.pushButton_6.setText(_translate("MainWindow", "PushButton"))

    def setupSlider(self):

        self.lims = np.array(self.axs[0].set_xlim())
        self.scroll.setPageStep(self.step * 100)
        self.scroll.actionTriggered.connect(self.updatePage)
        self.updatePage()

    def updatePage(self, evt = None):
        r = self.scroll.value() / ((1 + self.step) * 100)
        l1 = self.lims[0] + r * np.diff(self.lims)
        l2 = l1 + np.diff(self.lims) * self.step
        self.axs[0].set_xlim(l1, l2)
        print(self.scroll.value(), l1, l2)
        self.figure.canvas.draw_idle()

    #def stop_anim(self):
       #self.ani.event_source.stop()
       #self.ani.run ^= False

    #def onClick(event):
        #nonlocal anim_running
        #if anim_running:
            #ani.event_source.stop()
            #anim_running = False
        #else:
           # ani.event_source.start()
            #anim_running = True

if __name__ == '__main__':
    app = QtWidgets.QApplication.instance()
    if app is None:
        app = QtWidgets.QApplication(sys.argv)
        w1 = Ui_MainWindow()
        Form = QtWidgets.QMainWindow()
        w1.setupUi(Form)
        Form.show()
        app.exec_()
python
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    g_0008
    2022-07-21T18:38:29Z2022-07-21T18:38:29Z
     _prev_data = None
    
     global _prev_data
            if _prev_data == data.size:
                return
            else:
                _prev_data = data.size
            print(data.size)
    

    添加此代码并能够在 data = pd.DataFrame(data) <-- 这一行之后停止动画

    • 0

相关问题

  • 是否可以以某种方式自定义 QTabWidget?

  • telebot.anihelper.ApiException 错误

  • Python。检查一个数字是否是 3 的幂。输出 无

  • 解析多个响应

  • 交换两个数组的元素,以便它们的新内容也反转

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5