Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Integrating Matplotlib

Here are resources and additional documentation about Matplotlib and PySimpleGUI:

00:00 Integrating Matplotlib With PySimpleGUI. Creating graphs is a great way to share information with your colleagues. One of the most popular graphing packages for Python is Matplotlib.

00:13 Matplotlib can create all kinds of different graphs. If you’d like to know more about it, check out Python Plotting With Matplotlib. Matplotlib can be integrated with PySimpleGUI, so you can add graphs to your GUI fairly easily if you already know how to use Matplotlib.

00:32 If you don’t have Matplotlib installed, then you can do so using pip. Note that other libraries will be installed as well, including NumPy.

00:43 Now that you have all the pieces you need to write the code, you can create a new file called The demo code is a little long, so let’s look at it in pieces starting with this first section.

00:59 These are the imports you’ll need to make your code work. This code also sets up the Matplotlib Figure() and adds a plot using .add_subplot(). For more details, you may want to refer to the documentation.

01:17 The PySimpleGUIQt port doesn’t work in the same way currently, but that’s being worked on for a future release. In both PySimpleGUI and Tkinter, you use the Canvas() element for drawing.

01:29 You can read more about that in the documentation. Now you can create a helper function to draw the figure on PySimpleGUI’s Canvas(). A helper function is repetitive code that you don’t want to write multiple times.

01:43 Onscreen, you can see the helper function being written. It uses the .draw() method to draw the plots to PySimpleGUI’s Canvas() element.

01:52 To use PySimpleGUI’s Canvas(), you need to pass it into FigureCanvasTkAgg() along with the Matplotlib figure object. FigureCanvasTkAgg() comes from Matplotlib and is used by Tkinter to embed the plots in PySimpleGUI.

02:08 This would work differently if you were using PySimpleGUIQt. The last step is to write the user interface with PySimpleGUI. This is simpler than the previous image viewer interface, as the layout only consists of three elements.

02:26 To create the user interface, all you need is a Text() element, a Canvas() element, and a Button() element.

02:38 Then all of these are added to a Window(),

02:59 and you can call your draw_figure() helper function to draw the plot.

03:11 You don’t need the event loop here because you won’t be interacting with the elements in this user interface. Now you can see the program running.

03:27 There are other Matplotlib demos included with PySimpleGUI that you should check out. Now that you’ve seen how to integrate Matplotlib with PySimpleGUI, in the next section you’ll see how to integrate computer vision with the OpenCV library.

Become a Member to join the conversation.