Sometimes I want to make a simple (or complex) graphical user interface (GUI) for exploratory data analysis. I use Python, but there are probably better ways to do this. This tutorial serves as a basic walkthrough on how to produce a Python GUI that contains an interactive matplotlib figure.
A caveat before we start: There are a few different options available for GUIs in Python and elsewhere (Tkinter, Qt, Enthought Traits, etc). I’ve tried all of these at some point, but you shouldn’t take my word on what is described below as being “the best way” to do things; this is just what I have found to work best for me. Here is my setup (e.g., things you may need to make this work):
Anaconda Python installation. Both Python 2.7 and 3.x will work, but with 3.x you may need to do conda install pyside after you have installed Anaconda.
If you have these requirements already and just want to test the GUI, here is the “TL;DR”:
Let’s create a GUI
If you have Qt installed, then you should also have Qt Designer. Load it and create a new widget:
On the left you can see widgets, which you can drag and drop the background. Double-click buttons or text to edit them.
In Qt we use layouts and size policies to make things sit in the right spot. First let’s put our buttons into a “horizontal layout” and drop a horizontal spacer before the last button:
To put everything in the right spot (eventually..), right-click on the parent window and give it a “vertical layout”:
Now let’s add a default “widget” at the top, which will later become our matplotlib figure.
Let’s set the size policies so that the widget has MinimumExpanding policy for the vertical direction.
Produce Python code from your widget
In the Qt Designer, save your widget to a file with a .ui extension: my_gui.ui. Now from the terminal we will create a Python script called my_gui.py from our my_gui.ui file:
The my_gui.py file looks like this:
(I should note here that you can actually just import .ui files directly into Python through the QUiLoader class, but I have not shown that here just so you can see the “bare bones” of what the Python code looks like.)
Clean up the automatically-generated code
The code in our my_gui.py file is not quite ready yet. This is because the pyuic4 function is not perfectly suited for our needs, so we will have to change some of the code that it has generated. Specifically we should replace the PyQt imports to PySide, remove the translation function, and re-name our widgets. To save time, below is the complete updated file (you can check the line-by-line differences yourself). You should be able to run this code and show a blank widget by just typing python my_gui.py from the terminal.
Integrate matplotlib functionality
Now we are going to add a matplotlib widget and get Python code to be executed when we click buttons. First create a file called mpl.py that contains the following code:
And create a matplotlibrc file in the same folder, which lets you style your figures.
Add matplotlib functionality to our widget
Now let’s change our Widget to be a matplotlib widget (MPLWidget). In my_gui.py add import mpl at the top of the file and change this:
And we’ll set up the axes in the end of our __init__ function:
Connect signals to widgets
All widgets have signals that they emit when something happens in the GUI. For example, when a button is clicked, it emits a clicked signal. We just need to conncet these signals to a function we have written. Here’s one example:
Putting this all together, here is our completed my_gui.py file: