繁体   English   中英

在 PyQt 窗口中绘制没有滞后的 matplotlib 矩形

[英]Draw matplotlib rectangle without lag in PyQt window

当我单击鼠标时,我想绘制一个框。 当我通过单击按钮移动鼠标时,该框应该会发生变化。 现在,问题是除非我停止鼠标移动,否则框不会更新,因此运动似乎有点滞后。 如果不把情节放在 PyQt 环境中,它工作得很好。 但不幸的是,我需要在 PyQt 窗口中使用它。

我打印出鼠标的坐标,当按钮被点击时,它们会立即更新。

任何人都可以帮助或想到替代结构吗?

import sys
from PyQt4 import QtGui

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt


from matplotlib.patches import Rectangle



class Annotate(object):
    def __init__(self, ax):
        self.ax = ax
        self.rect = Rectangle((0,0), 0, 0)
        self.x0 = None
        self.y0 = None
        self.x1 = None
        self.y1 = None
        self.ax.add_patch(self.rect)
        self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press)
        self.ax.figure.canvas.mpl_connect('button_release_event', self.on_release)
        self.ax.figure.canvas.mpl_connect('motion_notify_event', self.on_motion)

        self.is_pressed = 0

    def on_motion(self, event):
        if self.is_pressed:
            self.x1 = event.xdata
            self.y1 = event.ydata 
            print(self.x1, self.y1)

            self.rect.set_width(self.x1 - self.x0)
            self.rect.set_height(self.y1 - self.y0)
            self.rect.set_xy((self.x0, self.y0))
            self.rect.figure.canvas.draw() 

    def on_press(self, event):
        self.is_pressed = 1
        self.x0 = event.xdata
        self.y0 = event.ydata

    def on_release(self, event):
        self.is_pressed = 0


class Window(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.figure = plt.figure(facecolor='white')
        self.canvas = FigureCanvas(self.figure)
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.canvas)
        self.setLayout(layout)

        ''' plot some random stuff '''
        ax = self.figure.add_subplot(111)
        self.ax = ax
        ax.plot()
        self.canvas.draw()

    def get_ax(self):
        return self.ax

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    main = Window()
    ax = main.get_ax()
    main.show()
    a = Annotate(ax)
    sys.exit(app.exec_())

我知道已经晚了:),但是试试这个:

import sys
from PyQt5 import QtWidgets

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt


from matplotlib.patches import Rectangle



class Annotate(object):
    def __init__(self, ax):
        self.ax = ax
        self.rect = Rectangle((0,0), 0, 0)
        self.x0 = None
        self.y0 = None
        self.x1 = None
        self.y1 = None
        self.ax.add_patch(self.rect)
        self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press)
        self.ax.figure.canvas.mpl_connect('button_release_event', self.on_release)
        self.ax.figure.canvas.mpl_connect('motion_notify_event', self.on_motion)

        self.is_pressed = 0

    def on_motion(self, event):
        if self.is_pressed:
            self.x1 = event.xdata
            self.y1 = event.ydata 
            print(self.x1, self.y1)

            self.rect.set_width(self.x1 - self.x0)
            self.rect.set_height(self.y1 - self.y0)
            self.rect.set_xy((self.x0, self.y0))
            self.rect.figure.canvas.draw() 

    def on_press(self, event):
        self.is_pressed = 1
        self.x0 = event.xdata
        self.y0 = event.ydata

    def on_release(self, event):
        self.is_pressed = 0


class Window(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.figure = plt.figure(facecolor='white')
        self.canvas = FigureCanvas(self.figure)
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.canvas)
        self.setLayout(layout)

        ''' plot some random stuff '''
        ax = self.figure.add_subplot(111)
        self.ax = ax
        ax.plot()
        self.canvas.draw()

    def get_ax(self):
        return self.ax

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    main = Window()
    ax = main.get_ax()
    main.show()
    a = Annotate(ax)
    sys.exit(app.exec_())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM