Linux GUI gets frozen when using MatPlotLib in Python
NickName:Matho Ask DateTime:2017-05-16T23:06:43

Linux GUI gets frozen when using MatPlotLib in Python

I have found a strange behavior in my PyQt application which freezes whole Graphical interface of my OS (tested on Linux Mint 17.1 and 18.0, Red Hat Enterprise Linux Server release 7.3). For Windows, it seems to behave normally (i.e., no freezing).

After clicking Click me button, some calculations start in method calculateSomething of our Worker, which works in different thread (in this case, just sleep for 3 seconds). Once it's done, signal is emitted to the MainWindow to display a matplotlib plot.

Everything works normal until you press File in the main menu and keep it opened while computation is in progress. Then, when the plot window pops up, whole Graphical interface is frozen.

Any ideas what's going on? Seems like strange bug for me. Or is there anything I am doing wrong? Thank you.

Here is an example code which demonstrates the behavior:

import sys
from time import sleep
from PyQt4 import QtGui, QtCore
import matplotlib.pyplot as plt

# -------------------------------------------------

# Simulates a Worker which does a longer time calculation
class Worker(QtCore.QObject):
    calculationFinished = QtCore.pyqtSignal()   # emited when calculation is finished   
    def __init__(self, parent=None):
        QtCore.QObject.__init__(self, parent)

        self.TheWorker = QtCore.QThread()
        self.moveToThread(self.TheWorker)
        self.TheWorker.start()

    def calculateSomething(self):
        sleep(3) # wait for 3 seconds
        self.calculationFinished.emit() # emit the signal

# -------------------------------------------------

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.one = QtGui.QAction("One", self)
        self.two = QtGui.QAction("Two", self)

        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('File')

        fileMenu.addAction(self.one)
        fileMenu.addAction(self.two)

        self.myWorker = Worker()
        self.myWorker.calculationFinished.connect(self.showPopUp)

        self.button = QtGui.QPushButton("Click me", self)
        self.button.clicked.connect(self.myWorker.calculateSomething)
        self.button.move(60, 30)

    def showPopUp(self):
        plt.plot([1,2,3,4])
        plt.ylabel('some numbers')
        plt.show()

app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())

Copyright Notice:Content Author:「Matho」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/44005176/linux-gui-gets-frozen-when-using-matplotlib-in-python

More about “Linux GUI gets frozen when using MatPlotLib in Python” related questions

Linux GUI gets frozen when using MatPlotLib in Python

I have found a strange behavior in my PyQt application which freezes whole Graphical interface of my OS (tested on Linux Mint 17.1 and 18.0, Red Hat Enterprise Linux Server release 7.3). For Window...

Show Detail

Set Matplotlib backend when using Windows Subsystem for Linux

My Matplotlib backend keeps reverting to TkAgg. This is a problem because in the Windows Subsystem for Linux (WSL), you can't do GUI stuff, and so I get the error TclError: no display name and no $

Show Detail

jupyter lab notebook gets frozen with matplotlib widget cursor

I would like to fill a ipywidgets.widgets.Output() with a matplotlib imshow and a matplotlib.widgets.Cursor for further processing when the user clicks on the point showed by the cursor. Here is th...

Show Detail

matplotlib equivalent for Ubuntu servers with no GUI?

I have a GUI-less cloud server running Bitnami-Django Ubuntu 14.04 LTS that is meant to retrieve and graph data for users, but it cannot produce the graphs. To be clear, I only care that the graph ...

Show Detail

Python Tkinter Embed Matplotlib in GUI

I'm trying to embed a plot in my Tkinter GUI coded in Python. I believe the code below succeeds in simply putting a graph into a canvas, but I don't have any control of the canvas location within t...

Show Detail

Tkinter: when clicking on button GUI is frozen

I am new using Tkinter to create GUIs. I would like to create a GUI which has a button that when pressed it executes some command lines. This is the code for creating the button: ros_start_button =

Show Detail

Python IPC with matplotlib

Project description: Connect existing "C" program (main control) to Python GUI/Widget. For this I'm using a FIFO. The C program is designed look at frame based telemetry. The Python GUI performs...

Show Detail

python opencv with docker: Matplotlib.show Matplotlib is currently using agg, which is a non-GUI backend

I need to run opencv + python in docker. This code should show a GUI popup with an image: import cv2 from matplotlib import pyplot as plt img = cv2.imread("image.jpg") img_gray = cv2.cvtC...

Show Detail

Weird TypeError when calling simple matplotlib function

I have been trying to execute some matplotlib.pyplot code and kept getting this random error: TypeError: int() argument must be a string, a bytes-like object or a real number, not 'KeyboardModifie...

Show Detail

Matplotlib for Python 3 and Linux

I understand that Matplotlib has been ported to Python 3 (matplotlib-python-3-thanks-cape-town-group). However, it seems that I can only find Windows executables. The matplotlib-py3 page claims t...

Show Detail