Matplotlib (http://matplotlib.sourceforge.net/) provides a great range of plotting tools and functionality. It is a nice alternative for Matlab. Making a plot with matplotlib in Python is not that difficult. But often you need to be able to embed a plot into a GUI with other components like push buttons and text fields and so on. This article shows you how to embed any Matplotlib on to a GUI made with PyQt.
First do File-> New and create a new Dialog form without buttons (we’ll add by ourselves). We’ll be making a simple GUI that plot some random numbers on a matplotlib graph with a push of a button. So first place a Push button on the dialog (drag and drop from the left pane). Now lets get ready to draw our plot. Since matplotlib is not part of PyQt it doesn’t know how to handle it, and it shouldn’t. All we need to do is make a place on our dialog for matplotlib to do it’s drawing. matplotlib will be a widget on the PyQt dialog. So select Widget (from Containers) from the left pane and drop in on our GUI. Now your PyQt would look like this:
Note in the Object Inspector (right-top) that the widget is of class QWidget. But our intention is to draw a matplotlib on it. So what we need to do is change the behavior of the widget. That is, instead of being of class type QWidget, we want it to be of a type that can draw a matplotlib graph. This is called promoting. We need to promote the widget to a type of our need. Doing that is pretty easy. Right-click on widget in Object Inspector (or on the widget itself) and select Promote to. Fill the two fields as shown:
You are allowed to use any name for the class name and header file name. This just tell PyQt that a class named MatplotlibWidget, contained in file matplotlibwidgetFile.py (no need to write the .py extension) will define the behavior of this widget. I purposely changed the class name and header file names to show that they do not need to be the same. Now click Add and Promote. Take a note of your widget type now in Object Inspector:
Note how the class type of the widget is now changed to matplotlibWidget, what we promoted it to. Now all we need to do is create a matplotlibwidgetFile.py and define the widgets behavior. But before doing that we need to translate this GUI code to Python code. Save this GUI as PlotGUI.ui. PlotGUI.ui is just an XML. You will have something similar to this:
To translate this to a .py file:
$ pyuic4 PlotGUI.ui > PlotGUI.py
You will have a Python file similar to this:
Note the final line in the file. It includes the matplotlibwidgetFile and import matplotlibWidget, the class that is going to define the behavior of this widget. These two lines,
define the two objects in our GUI: push button and the widget. Note that pushButton and widget will be their identifiers. We will be using these two names to access them.
So far we have created a PyQt GUI, added a push button and widget to it, and we have specified that the widgets behavior will be handled by matplotlibWidget class. What we need to do now is to define that class then. It is pretty easy:
What this file essentially doing is creating a canvas to draw and give it to our matplotlibWidget. So we now have defined how our plotting widget will be handled. All that is left is to actually draw something on it.
Here’s our main program that will draw random numbers on the matplotlib graph when we push the button:
Everytime we push the button it will call PlotFunc() and a random set of integers are drawn on the graph. If we break out the code, the first execution is at:
This part basically creates an instance of an QApplication to run our GUI instance. Second it creates as instance of GUIForm. This is our class that we do our processing and pass data to our widget to draw. When we create an instance if GUIForm it calls the initializer:
Here it first creates an instance of Ui_Dialog. Note that Ui_Dialog is the class name in PlotGUI.py, the Python file that was generated from our GUI file. So this is basically making an instance of our GUI and then sets it up by calling setupUi(self). Final line is where we connect the push button signal to the PlotFunc. Then after the GUI is built myapp.show() will render the GUI on screen. app.exec_() starts a loop where any action on the GUI (clicks, etc) will be caught and handled and passing it to sys.exit() makes sure when we close the GUI is exits gracefully. The PlotFunc in pretty self explanatory:
It accesses the widget object (remember that it is the name it is named in PlotGUI.py) to make the drawing. You will see something similar when you click the push button:
This is pretty much it for embedding a matplotlib graph on a PyQt GUI.
 Matplotlib for Python Developers
 Introduction to Python Programming and Developing GUI Applications With PyQT