【问题标题】:Adding a plot with event handlers to an empty layout将带有事件处理程序的绘图添加到空布局
【发布时间】:2019-07-24 19:06:37
【问题描述】:

我制作了一个交互式绘图,您可以单击它来绘制点。我想将它添加到 PyQt5 GUI,但我不确定如何将所述绘图链接到我在 GUI 中创建的画布。

from PyQt5 import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.pyplot import Figure
import matplotlib.pyplot as plt

class MyWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()
        central = QtWidgets.QWidget(self)
        self.VL = QtWidgets.QVBoxLayout(central)
        self.canvas = FigureCanvas(Figure())
        self.VL.addWidget(self.canvas)

        self.setWindowTitle("Click to Plot")
        self.setCentralWidget(central)

        self.canvas = FigureCanvas(Figure())
        self.VL.addWidget(self.canvas)
        self.ax1f1 = self.canvas.figure.subplots()

        self.ax1f1.set_ylim([0, 100])
        self.ax1f1.set_xlim([0, 0.5])

    class LineBuilder:
        def __init__(self, line):
            self.line = line
            self.xs = list(line.get_xdata())
            self.ys = list(line.get_ydata())
            self.cid = line.figure.canvas.mpl_connect('button_press_event', self.click)

        def click(self, event):
            if event.inaxes != self.line.axes:
                return
            self.xs.append(event.xdata)
            self.ys.append(event.ydata)
            self.line.set_data(self.xs, self.ys)
            self.line.figure.canvas.draw()

    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.set_title('Click to add a point')
    line, = ax.plot([], [], 'o')  # empty point
    linebuilder = LineBuilder(line)

    plt.show()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

我没有收到错误,图表显示正常,但不在 GUI 内。

【问题讨论】:

    标签: python matplotlib pyqt5


    【解决方案1】:

    我不明白您为什么要创建多个图形,而您几乎只需创建一个图形,将其添加到画布,然后将该图形用于其余代码

    from PyQt5 import QtWidgets
    from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
    from matplotlib.pyplot import Figure
    import matplotlib.pyplot as plt
    
    
    class MyWindow(QtWidgets.QMainWindow):
        def __init__(self):
            super(MyWindow, self).__init__()
            central = QtWidgets.QWidget(self)
            self.VL = QtWidgets.QVBoxLayout(central)
            self.fig = Figure()
            self.ax = self.fig.add_subplot(111)
            self.ax.set_title('Click to add a point')
            self.line, = self.ax.plot([], [], 'o')  # empty point
    
            self.canvas = FigureCanvas(self.fig)
            self.VL.addWidget(self.canvas)
    
            self.setWindowTitle("Click to Plot")
            self.setCentralWidget(central)
    
            self.ax.set_ylim([0, 100])
            self.ax.set_xlim([0, 0.5])
    
            self.LB = LineBuilder(self.line)
    
    
    class LineBuilder:
        def __init__(self, line):
            self.line = line
            self.xs = list(line.get_xdata())
            self.ys = list(line.get_ydata())
            self.cid = line.figure.canvas.mpl_connect('button_press_event', self.click)
    
        def click(self, event):
            if event.inaxes != self.line.axes:
                return
            self.xs.append(event.xdata)
            self.ys.append(event.ydata)
            self.line.set_data(self.xs, self.ys)
            self.line.figure.canvas.draw()
    
    
    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        window = MyWindow()
        window.show()
        sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-26
      • 2018-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多