久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<small id='XvStX'></small><noframes id='XvStX'>

  • <i id='XvStX'><tr id='XvStX'><dt id='XvStX'><q id='XvStX'><span id='XvStX'><b id='XvStX'><form id='XvStX'><ins id='XvStX'></ins><ul id='XvStX'></ul><sub id='XvStX'></sub></form><legend id='XvStX'></legend><bdo id='XvStX'><pre id='XvStX'><center id='XvStX'></center></pre></bdo></b><th id='XvStX'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='XvStX'><tfoot id='XvStX'></tfoot><dl id='XvStX'><fieldset id='XvStX'></fieldset></dl></div>
      <legend id='XvStX'><style id='XvStX'><dir id='XvStX'><q id='XvStX'></q></dir></style></legend>

          <bdo id='XvStX'></bdo><ul id='XvStX'></ul>
      1. <tfoot id='XvStX'></tfoot>
      2. 如何在 PyQt5 GUI 中制作快速 matplotlib 實時繪圖

        How to make a fast matplotlib live plot in a PyQt5 GUI(如何在 PyQt5 GUI 中制作快速 matplotlib 實時繪圖)

            <tbody id='sZDYy'></tbody>

          • <legend id='sZDYy'><style id='sZDYy'><dir id='sZDYy'><q id='sZDYy'></q></dir></style></legend>

              <small id='sZDYy'></small><noframes id='sZDYy'>

              <tfoot id='sZDYy'></tfoot>
            1. <i id='sZDYy'><tr id='sZDYy'><dt id='sZDYy'><q id='sZDYy'><span id='sZDYy'><b id='sZDYy'><form id='sZDYy'><ins id='sZDYy'></ins><ul id='sZDYy'></ul><sub id='sZDYy'></sub></form><legend id='sZDYy'></legend><bdo id='sZDYy'><pre id='sZDYy'><center id='sZDYy'></center></pre></bdo></b><th id='sZDYy'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='sZDYy'><tfoot id='sZDYy'></tfoot><dl id='sZDYy'><fieldset id='sZDYy'></fieldset></dl></div>
                  <bdo id='sZDYy'></bdo><ul id='sZDYy'></ul>
                  本文介紹了如何在 PyQt5 GUI 中制作快速 matplotlib 實時繪圖的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  幾年前,我已經嘗試在 PyQt5 GUI 中嵌入實時 matplotlib 圖.實時圖顯示了從傳感器捕獲的實時數據流、一些過程……我得到了它的工作,您可以在此處閱讀相關帖子:

                  •  

                    2.第二個例子

                    我在這里找到了另一個實時 matplotlib 圖示例:
                    這沒什么大不了的,但我只是想知道為什么.

                    解決方案

                    第二種情況(使用FuncAnimation)更快,因為它使用blitting",這可以避免重繪幀之間不會改變的東西.

                    matplotlib 網站上提供的嵌入 qt 的示例沒有考慮速度,因此性能較差.您會注意到它在每次迭代時調用 ax.clear()ax.plot(),導致每次都重新繪制整個畫布.如果要使用與 FuncAnimation 中的代碼相同的代碼(也就是說,創(chuàng)建一個 Axes 和一個藝術家,并更新藝術家中的數據,而不是每次都創(chuàng)建一個新藝術家)你應該得到非常接近我相信的相同性能.

                    Some years ago, I already experimented with embedding live matplotlib plots in a PyQt5 GUI. Live plots show a data-stream real-time, captured from a sensor, some process, ... I got that working, and you can read the related posts here:

                    • Matplotlib animation inside your own GUI

                    • How do I plot in real-time in a while loop using matplotlib?

                    Now I need to do the same thing again. I remember my previous approach worked, but couldn't keep up with fast datastreams. I found a couple of example codes on the internet, that I'd like to present to you. One of them is clearly faster than the other, but I don't know why. I'd like to gain more insights. I believe a deeper understanding will enable me to keep my interactions with PyQt5 and matplotlib efficient.

                     

                    1. First example

                    This example is based on this article:
                    https://matplotlib.org/3.1.1/gallery/user_interfaces/embedding_in_qt_sgskip.html
                    The article is from the official matplotlib website, and explains how to embed a matplotlib figure in a PyQt5 window.

                    I did a few minor adjustments to the example code, but the basics are still the same. Please copy-paste the code below to a Python file and run it:

                    #####################################################################################
                    #                                                                                   #
                    #                PLOT A LIVE GRAPH IN A PYQT WINDOW                                 #
                    #                EXAMPLE 1                                                          #
                    #               ------------------------------------                                #
                    # This code is inspired on:                                                         #
                    # https://matplotlib.org/3.1.1/gallery/user_interfaces/embedding_in_qt_sgskip.html  #
                    #                                                                                   #
                    #####################################################################################
                    
                    from __future__ import annotations
                    from typing import *
                    import sys
                    import os
                    from matplotlib.backends.qt_compat import QtCore, QtWidgets
                    # from PyQt5 import QtWidgets, QtCore
                    from matplotlib.backends.backend_qt5agg import FigureCanvas
                    # from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
                    import matplotlib as mpl
                    import numpy as np
                    
                    class ApplicationWindow(QtWidgets.QMainWindow):
                        '''
                        The PyQt5 main window.
                    
                        '''
                        def __init__(self):
                            super().__init__()
                            # 1. Window settings
                            self.setGeometry(300, 300, 800, 400)
                            self.setWindowTitle("Matplotlib live plot in PyQt - example 1")
                            self.frm = QtWidgets.QFrame(self)
                            self.frm.setStyleSheet("QWidget { background-color: #eeeeec; }")
                            self.lyt = QtWidgets.QVBoxLayout()
                            self.frm.setLayout(self.lyt)
                            self.setCentralWidget(self.frm)
                    
                            # 2. Place the matplotlib figure
                            self.myFig = MyFigureCanvas(x_len=200, y_range=[0, 100], interval=20)
                            self.lyt.addWidget(self.myFig)
                    
                            # 3. Show
                            self.show()
                            return
                    
                    class MyFigureCanvas(FigureCanvas):
                        '''
                        This is the FigureCanvas in which the live plot is drawn.
                    
                        '''
                        def __init__(self, x_len:int, y_range:List, interval:int) -> None:
                            '''
                            :param x_len:       The nr of data points shown in one plot.
                            :param y_range:     Range on y-axis.
                            :param interval:    Get a new datapoint every .. milliseconds.
                    
                            '''
                            super().__init__(mpl.figure.Figure())
                            # Range settings
                            self._x_len_ = x_len
                            self._y_range_ = y_range
                    
                            # Store two lists _x_ and _y_
                            self._x_ = list(range(0, x_len))
                            self._y_ = [0] * x_len
                    
                            # Store a figure ax
                            self._ax_ = self.figure.subplots()
                    
                            # Initiate the timer
                            self._timer_ = self.new_timer(interval, [(self._update_canvas_, (), {})])
                            self._timer_.start()
                            return
                    
                        def _update_canvas_(self) -> None:
                            '''
                            This function gets called regularly by the timer.
                    
                            '''
                            self._y_.append(round(get_next_datapoint(), 2))     # Add new datapoint
                            self._y_ = self._y_[-self._x_len_:]                 # Truncate list _y_
                            self._ax_.clear()                                   # Clear ax
                            self._ax_.plot(self._x_, self._y_)                  # Plot y(x)
                            self._ax_.set_ylim(ymin=self._y_range_[0], ymax=self._y_range_[1])
                            self.draw()
                            return
                    
                    # Data source
                    # ------------
                    n = np.linspace(0, 499, 500)
                    d = 50 + 25 * (np.sin(n / 8.3)) + 10 * (np.sin(n / 7.5)) - 5 * (np.sin(n / 1.5))
                    i = 0
                    def get_next_datapoint():
                        global i
                        i += 1
                        if i > 499:
                            i = 0
                        return d[i]
                    
                    if __name__ == "__main__":
                        qapp = QtWidgets.QApplication(sys.argv)
                        app = ApplicationWindow()
                        qapp.exec_()
                    
                    

                    You should see the following window:

                     

                    2. Second example

                    I found another example of live matplotlib graphs here:
                    https://learn.sparkfun.com/tutorials/graph-sensor-data-with-python-and-matplotlib/speeding-up-the-plot-animation
                    However, the author doesn't use PyQt5 to embed his live plot. Therefore, I've modified the code a bit, to get the plot in a PyQt5 window:

                    #####################################################################################
                    #                                                                                   #
                    #                PLOT A LIVE GRAPH IN A PYQT WINDOW                                 #
                    #                EXAMPLE 2                                                          #
                    #               ------------------------------------                                #
                    # This code is inspired on:                                                         #
                    # https://learn.sparkfun.com/tutorials/graph-sensor-data-with-python-and-matplotlib/speeding-up-the-plot-animation  #
                    #                                                                                   #
                    #####################################################################################
                    
                    from __future__ import annotations
                    from typing import *
                    import sys
                    import os
                    from matplotlib.backends.qt_compat import QtCore, QtWidgets
                    # from PyQt5 import QtWidgets, QtCore
                    from matplotlib.backends.backend_qt5agg import FigureCanvas
                    # from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
                    import matplotlib as mpl
                    import matplotlib.figure as mpl_fig
                    import matplotlib.animation as anim
                    import numpy as np
                    
                    class ApplicationWindow(QtWidgets.QMainWindow):
                        '''
                        The PyQt5 main window.
                    
                        '''
                        def __init__(self):
                            super().__init__()
                            # 1. Window settings
                            self.setGeometry(300, 300, 800, 400)
                            self.setWindowTitle("Matplotlib live plot in PyQt - example 2")
                            self.frm = QtWidgets.QFrame(self)
                            self.frm.setStyleSheet("QWidget { background-color: #eeeeec; }")
                            self.lyt = QtWidgets.QVBoxLayout()
                            self.frm.setLayout(self.lyt)
                            self.setCentralWidget(self.frm)
                    
                            # 2. Place the matplotlib figure
                            self.myFig = MyFigureCanvas(x_len=200, y_range=[0, 100], interval=20)
                            self.lyt.addWidget(self.myFig)
                    
                            # 3. Show
                            self.show()
                            return
                    
                    class MyFigureCanvas(FigureCanvas, anim.FuncAnimation):
                        '''
                        This is the FigureCanvas in which the live plot is drawn.
                    
                        '''
                        def __init__(self, x_len:int, y_range:List, interval:int) -> None:
                            '''
                            :param x_len:       The nr of data points shown in one plot.
                            :param y_range:     Range on y-axis.
                            :param interval:    Get a new datapoint every .. milliseconds.
                    
                            '''
                            FigureCanvas.__init__(self, mpl_fig.Figure())
                            # Range settings
                            self._x_len_ = x_len
                            self._y_range_ = y_range
                    
                            # Store two lists _x_ and _y_
                            x = list(range(0, x_len))
                            y = [0] * x_len
                    
                            # Store a figure and ax
                            self._ax_  = self.figure.subplots()
                            self._ax_.set_ylim(ymin=self._y_range_[0], ymax=self._y_range_[1])
                            self._line_, = self._ax_.plot(x, y)
                    
                            # Call superclass constructors
                            anim.FuncAnimation.__init__(self, self.figure, self._update_canvas_, fargs=(y,), interval=interval, blit=True)
                            return
                    
                        def _update_canvas_(self, i, y) -> None:
                            '''
                            This function gets called regularly by the timer.
                    
                            '''
                            y.append(round(get_next_datapoint(), 2))     # Add new datapoint
                            y = y[-self._x_len_:]                        # Truncate list _y_
                            self._line_.set_ydata(y)
                            return self._line_,
                    
                    # Data source
                    # ------------
                    n = np.linspace(0, 499, 500)
                    d = 50 + 25 * (np.sin(n / 8.3)) + 10 * (np.sin(n / 7.5)) - 5 * (np.sin(n / 1.5))
                    i = 0
                    def get_next_datapoint():
                        global i
                        i += 1
                        if i > 499:
                            i = 0
                        return d[i]
                    
                    if __name__ == "__main__":
                        qapp = QtWidgets.QApplication(sys.argv)
                        app = ApplicationWindow()
                        qapp.exec_()
                    
                    

                    The resulting live plot is exactly the same. However, if you start playing around with the interval parameter from the MyFigureCanvas() constructor, you will notice that the first example won't be able to follow. The second example can go much faster.

                     

                    3. Questions

                    I've got a couple of questions I'd like to present to you:

                    • The QtCore and QtWidgets classes can be imported like this:
                      from matplotlib.backends.qt_compat import QtCore, QtWidgets
                      or like this:
                      from PyQt5 import QtWidgets, QtCore
                      Both work equally well. Is there a reason to prefer one over the other?
                       

                    • The FigureCanvas can be imported like this:
                      from matplotlib.backends.backend_qt5agg import FigureCanvas
                      or like this: from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
                      But I already figured out why. The backend_qt5agg file seems to define FigureCanvas as an alias for FigureCanvasQTAgg.
                       

                    • Why exactly is the second example so much faster than the first one? Honestly, it surprises me. The first example is based on a webpage from the official matplotlib website. I'd expect that one to be better.
                       

                    • Do you have any suggestions to make the second example even faster?

                     

                    4. Edits

                    Based on the webpage:
                    https://bastibe.de/2013-05-30-speeding-up-matplotlib.html
                    I modified the first example to increase its speed. Please have a look at the code:

                    #####################################################################################
                    #                                                                                   #
                    #                PLOT A LIVE GRAPH IN A PYQT WINDOW                                 #
                    #                EXAMPLE 1 (modified for extra speed)                               #
                    #               --------------------------------------                              #
                    # This code is inspired on:                                                         #
                    # https://matplotlib.org/3.1.1/gallery/user_interfaces/embedding_in_qt_sgskip.html  #
                    # and on:                                                                           #
                    # https://bastibe.de/2013-05-30-speeding-up-matplotlib.html                         #
                    #                                                                                   #
                    #####################################################################################
                    
                    from __future__ import annotations
                    from typing import *
                    import sys
                    import os
                    from matplotlib.backends.qt_compat import QtCore, QtWidgets
                    # from PyQt5 import QtWidgets, QtCore
                    from matplotlib.backends.backend_qt5agg import FigureCanvas
                    # from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
                    import matplotlib as mpl
                    import numpy as np
                    
                    class ApplicationWindow(QtWidgets.QMainWindow):
                        '''
                        The PyQt5 main window.
                    
                        '''
                        def __init__(self):
                            super().__init__()
                            # 1. Window settings
                            self.setGeometry(300, 300, 800, 400)
                            self.setWindowTitle("Matplotlib live plot in PyQt - example 1 (modified for extra speed)")
                            self.frm = QtWidgets.QFrame(self)
                            self.frm.setStyleSheet("QWidget { background-color: #eeeeec; }")
                            self.lyt = QtWidgets.QVBoxLayout()
                            self.frm.setLayout(self.lyt)
                            self.setCentralWidget(self.frm)
                    
                            # 2. Place the matplotlib figure
                            self.myFig = MyFigureCanvas(x_len=200, y_range=[0, 100], interval=1)
                            self.lyt.addWidget(self.myFig)
                    
                            # 3. Show
                            self.show()
                            return
                    
                    class MyFigureCanvas(FigureCanvas):
                        '''
                        This is the FigureCanvas in which the live plot is drawn.
                    
                        '''
                        def __init__(self, x_len:int, y_range:List, interval:int) -> None:
                            '''
                            :param x_len:       The nr of data points shown in one plot.
                            :param y_range:     Range on y-axis.
                            :param interval:    Get a new datapoint every .. milliseconds.
                    
                            '''
                            super().__init__(mpl.figure.Figure())
                            # Range settings
                            self._x_len_ = x_len
                            self._y_range_ = y_range
                    
                            # Store two lists _x_ and _y_
                            self._x_ = list(range(0, x_len))
                            self._y_ = [0] * x_len
                    
                            # Store a figure ax
                            self._ax_ = self.figure.subplots()
                            self._ax_.set_ylim(ymin=self._y_range_[0], ymax=self._y_range_[1]) # added
                            self._line_, = self._ax_.plot(self._x_, self._y_)                  # added
                            self.draw()                                                        # added
                    
                            # Initiate the timer
                            self._timer_ = self.new_timer(interval, [(self._update_canvas_, (), {})])
                            self._timer_.start()
                            return
                    
                        def _update_canvas_(self) -> None:
                            '''
                            This function gets called regularly by the timer.
                    
                            '''
                            self._y_.append(round(get_next_datapoint(), 2))     # Add new datapoint
                            self._y_ = self._y_[-self._x_len_:]                 # Truncate list y
                    
                            # Previous code
                            # --------------
                            # self._ax_.clear()                                   # Clear ax
                            # self._ax_.plot(self._x_, self._y_)                  # Plot y(x)
                            # self._ax_.set_ylim(ymin=self._y_range_[0], ymax=self._y_range_[1])
                            # self.draw()
                    
                            # New code
                            # ---------
                            self._line_.set_ydata(self._y_)
                            self._ax_.draw_artist(self._ax_.patch)
                            self._ax_.draw_artist(self._line_)
                            self.update()
                            self.flush_events()
                            return
                    
                    # Data source
                    # ------------
                    n = np.linspace(0, 499, 500)
                    d = 50 + 25 * (np.sin(n / 8.3)) + 10 * (np.sin(n / 7.5)) - 5 * (np.sin(n / 1.5))
                    i = 0
                    def get_next_datapoint():
                        global i
                        i += 1
                        if i > 499:
                            i = 0
                        return d[i]
                    
                    if __name__ == "__main__":
                        qapp = QtWidgets.QApplication(sys.argv)
                        app = ApplicationWindow()
                        qapp.exec_()
                    
                    

                    The result is pretty amazing. The modifications make the first example definitely much faster! However, I don't know if this makes the first example equally fast now to the second example. They're certainly close to each other. Anyone an idea who wins?

                    Also, I noticed that one vertical line on the left, and one horizontal line on top is missing: It's not a big deal, but I just wonder why.

                    解決方案

                    The second case (using FuncAnimation) is faster because it uses "blitting", which avoids redrawing things that do not change between frames.

                    The example provided on the matplotlib website for embedding in qt was not written with speed in mind, hence the poorer performance. You'll notice that it calls ax.clear() and ax.plot() at each iteration, causing the whole canvas to be redrawn everytime. If you were to use the same code as in the code with FuncAnimation (that is to say, create an Axes and an artist, and update the data in the artist instead of creating a new artists every time) you should get pretty close to the same performance I believe.

                    這篇關于如何在 PyQt5 GUI 中制作快速 matplotlib 實時繪圖的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                    【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How to bind a function to an Action from Qt menubar?(如何將函數綁定到 Qt 菜單欄中的操作?)
                  PyQt progress jumps to 100% after it starts(PyQt 啟動后進度躍升至 100%)
                  How to set yaxis tick label in a fixed position so that when i scroll left or right the yaxis tick label should be visible?(如何將 yaxis 刻度標簽設置在固定位置,以便當我向左或向右滾動時,yaxis 刻度標簽應該可見
                  `QImage` constructor has unknown keyword `data`(`QImage` 構造函數有未知關鍵字 `data`)
                  Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                  How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時顯示進度條?)

                    • <bdo id='jtx58'></bdo><ul id='jtx58'></ul>

                        <small id='jtx58'></small><noframes id='jtx58'>

                          <tbody id='jtx58'></tbody>
                        <i id='jtx58'><tr id='jtx58'><dt id='jtx58'><q id='jtx58'><span id='jtx58'><b id='jtx58'><form id='jtx58'><ins id='jtx58'></ins><ul id='jtx58'></ul><sub id='jtx58'></sub></form><legend id='jtx58'></legend><bdo id='jtx58'><pre id='jtx58'><center id='jtx58'></center></pre></bdo></b><th id='jtx58'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='jtx58'><tfoot id='jtx58'></tfoot><dl id='jtx58'><fieldset id='jtx58'></fieldset></dl></div>
                        <legend id='jtx58'><style id='jtx58'><dir id='jtx58'><q id='jtx58'></q></dir></style></legend>
                      • <tfoot id='jtx58'></tfoot>

                          1. 主站蜘蛛池模板: 欧美视频1区 | 黑人巨大精品欧美一区二区免费 | 久久久久91 | 99精品免费久久久久久日本 | 久久精品一二三影院 | 日日夜夜狠狠操 | 99riav3国产精品视频 | 亚洲一区二区在线 | 亚州午夜精品 | 日韩精品一区二区三区在线播放 | 色呦呦网站 | 欧美日韩福利视频 | 欧美色综合一区二区三区 | 国久久 | 亚洲视频在线观看一区二区三区 | 日本一级淫片免费啪啪3 | 91大神在线资源观看无广告 | 欧美日韩国产中文 | 日韩在线国产 | 久久免费国产 | 日韩中文字幕视频在线观看 | 日本成人区 | 不卡在线视频 | 自拍偷拍一区二区三区 | 成人小视频在线免费观看 | 成人在线免费 | 国产精品av久久久久久久久久 | 欧美猛交 | 特一级毛片 | 日韩欧美中文字幕在线视频 | 欧美日韩中文在线 | 黄a在线观看 | 精品一区二区三区在线观看 | 亚洲精品久久久久久国产精华液 | 精品福利一区 | 天天操网| 一级全黄视频 | 日韩不卡一二区 | 人人澡人人射 | 国产乱码久久久久久 | 成人午夜精品一区二区三区 |