如何获得动态更新的图表?

时间:2019-06-28 21:01:25

标签: python pyqt pyqt5 qt-designer

我的ui文件包含一个小部件容器,其垂直布局名为“ VL”,而行编辑名为“ Radiance”。我创建了一个条形图,当我在折线编辑中输入值时要更改。目前,它只是这样做,只是每次都会创建一个新图。如果使用我的“删除”功能,它不会绘制完整的图,但是会破坏该图的布局。我认为问题出在我的“删除”功能以及放在哪里,请帮忙。

我导入了QtWidgets,uic,matplot.figure和必要的后端:

class MyWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()
        uic.loadUi('PyQt_App1.ui', self)
        self.setWindowTitle("Window Title")
        self.Radiance.textChanged.connect(self.animate)

    def animate(self):
        self.remove()
        r = self.Radiance.text()
        if r:
            rad = float(r)
            positions = [0.25]
            fig1 = Figure()
            ax1f1 = fig1.add_subplot(111)
            ax1f1.set_ylim([0, 100])
            ax1f1.set_xlim([0, 0.5])
            ax1f1.bar(positions, rad, width=0.2, color="g")
            self.addmpl(fig1)

        else:
            r = 0
            rad = float(r)
            positions = [0.25]
            fig1 = Figure()
            ax1f1 = fig1.add_subplot(111)
            ax1f1.set_ylim([0, 100])
            ax1f1.set_xlim([0, 0.5])
            ax1f1.bar(positions, rad, width=0.2, color="g")
            self.addmpl(fig1)

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.VL.addWidget(self.canvas)
        # self.canvas.setParent(self.Frame)
        self.canvas.draw()

    def remove(self):
        self.VL.removeWidget(self.canvas)
        self.canvas.close()


if __name__ == '__main__':
    import sys
    from PyQt5 import QtWidgets

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

1 个答案:

答案 0 :(得分:0)

我不会每次都创建一个新图形,而只是保留对当前条形图和当前轴的引用,并使用它们来更新图形,例如

from PyQt5 import QtWidgets

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.pyplot import Figure

class MyWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()
        central = QtWidgets.QWidget(self)
        self.VL = QtWidgets.QVBoxLayout(central)
        self.Radiance = QtWidgets.QLineEdit(self)
        self.VL.addWidget(self.Radiance)
        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])
        self.bar = None

        self.setWindowTitle("Window Title")
        self.setCentralWidget(central)
        self.Radiance.textChanged.connect(self.animate)

    def animate(self):
        r = self.Radiance.text()
        try:
            rad = float(r)
        except ValueError:
            rad = 0
        positions = [0.25]
        if self.bar:
            self.bar.remove()
        self.bar = self.ax1f1.bar(positions, rad, width=0.2, color="g")
        self.canvas.draw()

if __name__ == '__main__':
    import sys
    from PyQt5 import QtWidgets

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