Python qt5 matplotlb画布动画(带手动blit)

Python qt5 matplotlb画布动画(带手动blit),python,matplotlib,pyqt5,Python,Matplotlib,Pyqt5,因此,我想将qt5中的matplotlib画布与手动blit集成。 我发现了这个线索: 投票后的答案看起来很好,但是我需要它在qt5窗口中 因此,我尝试将上面的代码与matplotlib qt5教程合并到一个脚本中。 它有点工作,但是动画只在使用平移/缩放时工作,背景为黑色:D,如果blit设置为false,它甚至不会绘制 如果有人能帮助我,那将是令人惊奇的:)它被搞笑地打破了 from __future__ import unicode_literals import random impor

因此,我想将qt5中的matplotlib画布与手动blit集成。 我发现了这个线索: 投票后的答案看起来很好,但是我需要它在qt5窗口中

因此,我尝试将上面的代码与matplotlib qt5教程合并到一个脚本中。 它有点工作,但是动画只在使用平移/缩放时工作,背景为黑色:D,如果blit设置为false,它甚至不会绘制

如果有人能帮助我,那将是令人惊奇的:)它被搞笑地打破了

from __future__ import unicode_literals
import random
import time
import matplotlib
from PyQt5.QtWidgets import QSizePolicy, QApplication, QWidget, QVBoxLayout
from matplotlib import pyplot as plt
import sys
import matplotlib
matplotlib.use('Qt5Agg')

from matplotlib.animation import FuncAnimation
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import numpy as np


class MyMplCanvas(FigureCanvas):
    # Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.).
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        self.fig = plt.figure()

        FigureCanvas.__init__(self, self.fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self,
                                   QSizePolicy.Expanding,
                                   QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

        self.x = np.linspace(0, 50., num=100)
        self.X, self.Y = np.meshgrid(self.x, self.x)
        # self.fig = plt.figure()
        self.ax1 = self.fig.add_subplot(2, 1, 1)
        self.ax2 = self.fig.add_subplot(2, 1, 2)

        self.img = self.ax1.imshow(self.X, vmin=-1, vmax=1, interpolation="None", cmap="RdBu")

        self.line, = self.ax2.plot([], lw=3)
        self.text = self.ax2.text(0.8, 0.5, "")

        self.ax2.set_xlim(self.x.min(), self.x.max())
        self.ax2.set_ylim([-1.1, 1.1])

        self.t_start = time.time()
        self.k = 0.

        #self.fig.canvas.draw()  # note that the first draw comes before setting data

        #self.update(blit=False)
        anim = FuncAnimation(self.fig, self.update, interval=20)

    def update(self, blit=True):
        if blit:
            # cache the background
            self.axbackground = self.fig.canvas.copy_from_bbox(self.ax1.bbox)
            self.ax2background = self.fig.canvas.copy_from_bbox(self.ax2.bbox)


        self.img.set_data(np.sin(self.X / 3. + self.k) * np.cos(self.Y / 3. + self.k))
        self.line.set_data(self.x, np.sin(self.x / 3. + self.k))
        self.k += 0.11
        if blit:
            # restore background
            self.fig.canvas.restore_region(self.axbackground)
            self.fig.canvas.restore_region(self.ax2background)

            # redraw just the points
            self.ax1.draw_artist(self.img)
            self.ax2.draw_artist(self.line)
            self.ax2.draw_artist(self.text)

            # fill in the axes rectangle
            self.fig.canvas.blit(self.ax1.bbox)
            self.fig.canvas.blit(self.ax2.bbox)

            # in this post http://bastibe.de/2013-05-30-speeding-up-matplotlib.html
            # it is mentionned that blit causes strong memory leakage.
            # however, I did not observe that.

        else:
            # redraw everything
            self.fig.canvas.draw()

            # self.fig.canvas.flush_events()
            # alternatively you could use
            # plt.pause(0.000000000001)
            # however plt.pause calls canvas.draw(), as can be read here:
            # http://bastibe.de/2013-05-30-speeding-up-matplotlib.html


class PlotDialog(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        self.plot_layout = QVBoxLayout(self)
        self.plot_canvas = MyMplCanvas(self, width=5, height=4, dpi=100)

        self.navi_toolbar = NavigationToolbar(self.plot_canvas, self)
        self.plot_layout.addWidget(self.plot_canvas)
        self.plot_layout.addWidget(self.navi_toolbar)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    dialog0 = PlotDialog()
    dialog0.show()
    sys.exit(app.exec_())