Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 窗口大小更改会导致覆盖我的条形图-Matplotlib_Python_Matplotlib_Pyqt_Pyqt5 - Fatal编程技术网

Python 窗口大小更改会导致覆盖我的条形图-Matplotlib

Python 窗口大小更改会导致覆盖我的条形图-Matplotlib,python,matplotlib,pyqt,pyqt5,Python,Matplotlib,Pyqt,Pyqt5,我有一个简单的GUI,它由PyQt5和两个QWidgets和两个按钮组成。每个按钮生成一个条形图。这部分功能完美,但当窗口大小改变时,左侧的条形图会覆盖右侧的条形图(即,我有两次相同的条形图)。如何防止这种覆盖 后端代码: from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.QtGui import * import matplotlib.pyplot as plt from matplotlib.backend

我有一个简单的GUI,它由
PyQt5
和两个
QWidgets
和两个按钮组成。每个按钮生成一个条形图。这部分功能完美,但当窗口大小改变时,左侧的条形图会覆盖右侧的条形图(即,我有两次相同的条形图)。如何防止这种覆盖

后端代码:

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from network_view import Ui_MainWindow
import sys

class Run_script(QMainWindow, Ui_MainWindow):
    def __init__(self,parent=None):
        super(Run_script,self).__init__(parent)
        self.setupUi(self)
        self.pushButton1.clicked.connect(self.view_graph1)
        self.pushButton2.clicked.connect(self.view_graph2)
        self.figure = plt.figure()
        self.graph1 = FigureCanvas(self.figure)
        self.gridLayout.addWidget(self.graph1, 1, 0, 1, 1)
        self.graph2 = FigureCanvas(self.figure)
        self.gridLayout.addWidget(self.graph2, 1, 1, 1, 1)

    def view_graph1(self):
        self.figure.clf()
        self.axes = self.figure.add_subplot(111)
        x1 = [1, 2, 3, 4, 5]
        y1 = [10, 20, 30, 40, 50]
        x2 = [1.5, 2.5, 3.5, 4.5, 5.5]
        y2 = [15, 25, 35, 45, 55]
        self.axes.bar(x1,y1,width=0.40)
        self.axes.bar(x2,y2,width=0.40)
        self.graph1.draw()

    def view_graph2(self):
        self.figure.clf()
        self.axes = self.figure.add_subplot(111)
        x1 = [1, 2, 3, 4, 5]
        y1 = [10, 20, 30, 40, 50]
        x2 = [1.25, 2.25, 3.25, 4.25, 5.25]
        y2 = [15, 25, 35, 45, 55]
        x3 = [1.5, 2.5, 3.5, 4.5, 5.5]
        y3 = [1, 2, 3, 4, 5]
        self.axes.bar(x1,y1,width=0.40)
        self.axes.bar(x2,y2,width=0.40)
        self.axes.bar(x3,y3,width=0.40)
        self.graph2.draw()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    prog = Run_script()
    prog.show()
    sys.exit(app.exec_())
前端代码:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.pushButton1 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton1.setObjectName("pushButton1")
        self.gridLayout.addWidget(self.pushButton1, 0, 0, 1, 1)
        self.pushButton2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton2.setObjectName("pushButton2")
        self.gridLayout.addWidget(self.pushButton2, 0, 1, 1, 1)
        self.widget1 = QtWidgets.QWidget(self.centralwidget)
        self.widget1.setObjectName("widget1")
        self.gridLayout.addWidget(self.widget1, 1, 0, 1, 1)
        self.widget2 = QtWidgets.QWidget(self.centralwidget)
        self.widget2.setObjectName("widget2")
        self.gridLayout.addWidget(self.widget2, 1, 1, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton1.setText(_translate("MainWindow", "PushButton"))
        self.pushButton2.setText(_translate("MainWindow", "PushButton"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

谢谢

问题的原因是您使用的是相同的
plt.figure()
,因此当您使用
clf()
时,请清理图形并添加新绘图,这就是您认为它被覆盖的原因

你为什么不在按下按钮后立即看到这种效果

因为没有通知视图您必须应用更改,但一旦更改大小,它就会用更新的数据重新绘制,这与使用
update()
时看到的情况相同

一种可能的解决方案是每个
图形都有一个
plt.figure()

更好的实现是创建自己的画布:

class MyCanvas(FigureCanvas):
    def __init__(self, *args, **kwargs):
        self.figure = plt.figure()
        FigureCanvas.__init__(self, self.figure)

class Run_script(QMainWindow, Ui_MainWindow):
    def __init__(self,parent=None):
        super(Run_script,self).__init__(parent)
        self.setupUi(self)
        self.pushButton1.clicked.connect(self.view_graph1)
        self.pushButton2.clicked.connect(self.view_graph2)
        self.graph1 = MyCanvas()
        self.gridLayout.addWidget(self.graph1, 1, 0, 1, 1)
        self.graph2 = MyCanvas()
        self.gridLayout.addWidget(self.graph2, 1, 1, 1, 1)

    def view_graph1(self):
        self.graph1.figure.clf()
        axes = self.graph1.figure.add_subplot(111)
        x1 = [1, 2, 3, 4, 5]
        y1 = [10, 20, 30, 40, 50]
        x2 = [1.5, 2.5, 3.5, 4.5, 5.5]
        y2 = [15, 25, 35, 45, 55]
        axes.bar(x1,y1,width=0.40)
        axes.bar(x2,y2,width=0.40)
        self.graph1.draw()

    def view_graph2(self):
        self.graph2.figure.clf()
        axes = self.graph2.figure.add_subplot(111)
        x1 = [1, 2, 3, 4, 5]
        y1 = [10, 20, 30, 40, 50]
        x2 = [1.25, 2.25, 3.25, 4.25, 5.25]
        y2 = [15, 25, 35, 45, 55]
        x3 = [1.5, 2.5, 3.5, 4.5, 5.5]
        y3 = [1, 2, 3, 4, 5]
        axes.bar(x1,y1,width=0.40)
        axes.bar(x2,y2,width=0.40)
        axes.bar(x3,y3,width=0.40)
        self.graph2.draw()

导致此问题的原因是您使用的是相同的
plt.figure()
,因此当您使用
clf()
时,请清理图形并添加新绘图,这就是您认为它被覆盖的原因

你为什么不在按下按钮后立即看到这种效果

因为没有通知视图您必须应用更改,但一旦更改大小,它就会用更新的数据重新绘制,这与使用
update()
时看到的情况相同

一种可能的解决方案是每个
图形都有一个
plt.figure()

更好的实现是创建自己的画布:

class MyCanvas(FigureCanvas):
    def __init__(self, *args, **kwargs):
        self.figure = plt.figure()
        FigureCanvas.__init__(self, self.figure)

class Run_script(QMainWindow, Ui_MainWindow):
    def __init__(self,parent=None):
        super(Run_script,self).__init__(parent)
        self.setupUi(self)
        self.pushButton1.clicked.connect(self.view_graph1)
        self.pushButton2.clicked.connect(self.view_graph2)
        self.graph1 = MyCanvas()
        self.gridLayout.addWidget(self.graph1, 1, 0, 1, 1)
        self.graph2 = MyCanvas()
        self.gridLayout.addWidget(self.graph2, 1, 1, 1, 1)

    def view_graph1(self):
        self.graph1.figure.clf()
        axes = self.graph1.figure.add_subplot(111)
        x1 = [1, 2, 3, 4, 5]
        y1 = [10, 20, 30, 40, 50]
        x2 = [1.5, 2.5, 3.5, 4.5, 5.5]
        y2 = [15, 25, 35, 45, 55]
        axes.bar(x1,y1,width=0.40)
        axes.bar(x2,y2,width=0.40)
        self.graph1.draw()

    def view_graph2(self):
        self.graph2.figure.clf()
        axes = self.graph2.figure.add_subplot(111)
        x1 = [1, 2, 3, 4, 5]
        y1 = [10, 20, 30, 40, 50]
        x2 = [1.25, 2.25, 3.25, 4.25, 5.25]
        y2 = [15, 25, 35, 45, 55]
        x3 = [1.5, 2.5, 3.5, 4.5, 5.5]
        y3 = [1, 2, 3, 4, 5]
        axes.bar(x1,y1,width=0.40)
        axes.bar(x2,y2,width=0.40)
        axes.bar(x3,y3,width=0.40)
        self.graph2.draw()

如前所述,程序中只有一个图形。因此,两个图是相同的。你需要有两个不同的数字。此外,不要在GUI中使用pyplot,因为这会让您很容易遇到这样的问题

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

class Run_script(QMainWindow, Ui_MainWindow):
    def __init__(self,parent=None):
        super(Run_script,self).__init__(parent)
        self.setupUi(self)
        self.pushButton1.clicked.connect(self.view_graph1)
        self.pushButton2.clicked.connect(self.view_graph2)
        self.figure1 = Figure()
        self.graph1 = FigureCanvas(self.figure1)
        self.gridLayout.addWidget(self.graph1, 1, 0, 1, 1)
        self.figure2 = Figure()
        self.graph2 = FigureCanvas(self.figure2)
        self.gridLayout.addWidget(self.graph2, 1, 1, 1, 1)

    def view_graph1(self):
        self.figure1.clf()
        self.axes = self.figure1.add_subplot(111)
        x1 = [1, 2, 3, 4, 5]
        y1 = [10, 20, 30, 40, 50]
        x2 = [1.5, 2.5, 3.5, 4.5, 5.5]
        y2 = [15, 25, 35, 45, 55]
        self.axes.bar(x1,y1,width=0.40)
        self.axes.bar(x2,y2,width=0.40)
        self.graph1.draw()

    def view_graph2(self):
        self.figure2.clf()
        self.axes = self.figure2.add_subplot(111)
        x1 = [1, 2, 3, 4, 5]
        y1 = [10, 20, 30, 40, 50]
        x2 = [1.25, 2.25, 3.25, 4.25, 5.25]
        y2 = [15, 25, 35, 45, 55]
        x3 = [1.5, 2.5, 3.5, 4.5, 5.5]
        y3 = [1, 2, 3, 4, 5]
        self.axes.bar(x1,y1,width=0.40)
        self.axes.bar(x2,y2,width=0.40)
        self.axes.bar(x3,y3,width=0.40)
        self.graph2.draw()

如前所述,程序中只有一个图形。因此,两个图是相同的。你需要有两个不同的数字。此外,不要在GUI中使用pyplot,因为这会让您很容易遇到这样的问题

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

class Run_script(QMainWindow, Ui_MainWindow):
    def __init__(self,parent=None):
        super(Run_script,self).__init__(parent)
        self.setupUi(self)
        self.pushButton1.clicked.connect(self.view_graph1)
        self.pushButton2.clicked.connect(self.view_graph2)
        self.figure1 = Figure()
        self.graph1 = FigureCanvas(self.figure1)
        self.gridLayout.addWidget(self.graph1, 1, 0, 1, 1)
        self.figure2 = Figure()
        self.graph2 = FigureCanvas(self.figure2)
        self.gridLayout.addWidget(self.graph2, 1, 1, 1, 1)

    def view_graph1(self):
        self.figure1.clf()
        self.axes = self.figure1.add_subplot(111)
        x1 = [1, 2, 3, 4, 5]
        y1 = [10, 20, 30, 40, 50]
        x2 = [1.5, 2.5, 3.5, 4.5, 5.5]
        y2 = [15, 25, 35, 45, 55]
        self.axes.bar(x1,y1,width=0.40)
        self.axes.bar(x2,y2,width=0.40)
        self.graph1.draw()

    def view_graph2(self):
        self.figure2.clf()
        self.axes = self.figure2.add_subplot(111)
        x1 = [1, 2, 3, 4, 5]
        y1 = [10, 20, 30, 40, 50]
        x2 = [1.25, 2.25, 3.25, 4.25, 5.25]
        y2 = [15, 25, 35, 45, 55]
        x3 = [1.5, 2.5, 3.5, 4.5, 5.5]
        y3 = [1, 2, 3, 4, 5]
        self.axes.bar(x1,y1,width=0.40)
        self.axes.bar(x2,y2,width=0.40)
        self.axes.bar(x3,y3,width=0.40)
        self.graph2.draw()
那是因为它们是一样的。您对这两者使用相同的数字。为了解决这个问题(1)在PyQt GUI中根本不要使用
pyplot
(2)创建两个图形,每个图形画布一个。这是因为它们是相同的。您对这两者使用相同的数字。为了解决这个问题(1)在PyQt GUI中根本不要使用
pyplot
(2)创建两个图形,每个图形画布一个。