【问题标题】:Matplot lib does not plot with PyQtMatplotlib 不使用 PyQt 绘图
【发布时间】:2015-01-15 18:38:34
【问题描述】:

我遇到了 PyQt 和 Mathplotlib 的问题。 在这里,您可以找到我正在做的事情的伪代码:我有一个“MainWindow”类,它创建一个带有菜单的主窗口和一个空的 mathplotlib 图。当我单击菜单项时,执行方法“选择”,打开一个新对话框。还有一种方法可以在图形上绘制全局变量 Data 的内容。

import TeraGui
Data = []

class MainWindow(QMainWindow, TeraGui.Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.actionSelect.triggered.connect(self.Select)

        # Create the frame with the graphs
        self.create_main_frame()
        #Plot empty graphs
        self.axes.clear()
        self.canvas.draw()

    def create_main_frame(self):
        self.main_frame = QWidget()
        # Create the mpl Figure and FigCanvas objects.
        # 5x4 inches, 100 dots-per-inch
        #
        self.dpi = 100
        self.fig = Figure((5.0, 4.0), dpi=self.dpi)
        self.canvas = FigureCanvas(self.fig)
        self.canvas.setParent(self.main_frame)
        #
        self.axes = self.fig.add_subplot(111)
        # Create the navigation toolbar, tied to the canvas
        #
        self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame)
        #
        # Layout with box sizers
        #
        vbox = QVBoxLayout()
        vbox.addWidget(self.canvas)
        vbox.addWidget(self.mpl_toolbar)
        self.main_frame.setLayout(vbox)
        self.setCentralWidget(self.main_frame)

    def Plotting(self):
        """ Redraws the figure
        """
        print "I am here"
        time = Data[0]
        sig = Data[]

        plot(time, sig)

        # clear the axes and redraw the plot anew
        #
        self.axes.clear()
        self.axes.plot(time, sig)
        self.canvas.draw()

    def Select(self):
        dialog = Dialog(self)
        dialog.exec_()

现在,如果我在 MainWindow 类的 init 方法中添加这些行:

Global Data
Data = [[1,2,3],[4,5,6]]
self.Plotting()

“我在这里”已打印,并且绘图正确显示在图表中,但是如果我不添加这些行并且我尝试从 Dialog 类调用 Plotting 它将不起作用。 “我在这里”被绘制,但情节保持空白。在Dialog类中,当按下按钮框的“ok”按钮时调用“accept”方法:

class Dialog(QDialog, TeraGui.Ui_SelectFiles):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        self.setAttribute(Qt.WA_DeleteOnClose)
        self.setupUi(self)

    def accept(self):
        global Data
        Data = [[1,2,3],[4,5,6]]
        MainWindow().Plotting()

Plotting 方法还通过命令“plot(time,sig)”绘制单独的图。无论调用 Plotting 的方式如何,此图始终正确显示。

这是我第一次尝试使用 PyQt 和 matplotlib,但我无法识别错误。

【问题讨论】:

    标签: matplotlib pyqt


    【解决方案1】:

    问题出在线路上

    MainWindow().Plotting()
    

    当您编写MainWindow() 时,您实际上是在创建MainWindow 类的新实例并调用其Plotting() 函数,而不是现有MainWindow 实例之一。此窗口永远不会显示,并且由于您不保存对它的引用,因此随后会在accept() 返回时被删除。它存在的唯一证据是它写入控制台的'i am here' 消息。这就是你看不到情节的原因。

    在这种情况下,您将 MainWindow 实例设置为 dialogdialog = Dialog(self) 的父实例,因此您可以通过调用 parent() 来访问它。

    self.parent().Plotting()
    

    您还应该考虑向Plotting() 函数添加另一个参数,这样您就可以将数据直接传递给它,而不必在任何地方声明全局变量。

    def Plotting(self, Data):
        ...
    

    【讨论】:

    • 同意,+1 鼓励 OP 远离全局变量。不需要全局变量!
    猜你喜欢
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2020-04-09
    • 2015-08-19
    • 2015-06-23
    • 2013-10-23
    相关资源
    最近更新 更多