0

I'm having a problem with PyQt and Mathplotlib. Here you can find a pseudocode of what I am doing: I have a class "MainWindow" that creates a main window with a menu and an empty mathplotlib graph. When I click on the menu Item, the method "Select" is executed that opens a new Dialog. There is also a method that plots on the grah the content of the global variable 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_()

Now, if I add in the init method of the MainWindow class these lines:

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

"I am here" is printed and the plot is correctly displayed into the graph, BUT if I don't add these lines and i try to call Plotting from the Dialog class it doesn't work. "I am here" is plotted but the plot stays empty. In the Dialog class, method "accept" is caled when the "ok" button of a button box is pressed:

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

The Plotting method draws also a separate plot by means of the command "plot(time,sig)". This plot is always showed correctly regardless the way used to call Plotting.

These are my fist tries with PyQt and matplotlib and I am not able to identify the mistake.

1 Answer 1

1

The problem is with the line

MainWindow().Plotting()

When you write MainWindow() you are actually creating a new instance of the MainWindow class and calling its Plotting() function, not the one of your existing MainWindow instance. This window is never shown, and since you don't save a reference to it, is subsequently deleted when accept() returns. The only evidence of its existence is the 'i am here' message it writes to the console. This is why you don't see the plot.

In this case, you are setting your MainWindow instance as the parent of dialog through dialog = Dialog(self), so you could access it though a call to parent().

self.parent().Plotting()

You should also consider adding another parameter to the Plotting() function so you can pass your data directly to it instead of having to declare globals everywhere.

def Plotting(self, Data):
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

Agreed, +1 for encouraging the OP to stay away from global variables. No need for global variables!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.