简体   繁体   中英

Adding a plot with event handlers to an empty layout

I made an interactive plot that you click on to plot points. I want to add it to a PyQt5 GUI, but I am not sure how to link said plot to the canvas I created in my 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_())

I don't get an error, the graph shows up fine, but not inside the GUI.

I don't understand why you were creating several figure, when you pretty much just have to create one figure, add it to your canvas, and then use that figure for the rest of you code

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_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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