Reader small image

You're reading from  Matplotlib for Python Developers. - Second Edition

Product typeBook
Published inApr 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788625173
Edition2nd Edition
Languages
Right arrow
Authors (3):
Aldrin Yim
Aldrin Yim
author image
Aldrin Yim

Aldrin Yim is a PhD candidate and Markey Scholar in the Computation and System Biology program at Washington University, School of Medicine. His research focuses on applying big data analytics and machine learning approaches in studying neurological diseases and cancer. He is also the founding CEO of Codex Genetics Limited, which provides precision medicine solutions to patients and hospitals in Asia.
Read more about Aldrin Yim

Claire Chung
Claire Chung
author image
Claire Chung

Claire Chung is pursuing her PhD degree as a Bioinformatician at the Chinese University of Hong Kong. She enjoys using Python daily for work and lifehack. While passionate in science, her challenge-loving character motivates her to go beyond data analytics. She has participated in web development projects, as well as developed skills in graphic design and multilingual translation. She led the Campus Network Support Team in college, and shared her experience in data visualization in PyCon HK 2017.
Read more about Claire Chung

Allen Yu
Allen Yu
author image
Allen Yu

Allen Yu, PhD, is a Chevening Scholar, 2017-18, and an MSC student in computer science at the University of Oxford. He holds a PhD degree in Biochemistry from the Chinese University of Hong Kong, and he has used Python and Matplotlib extensively during his 10 years of bioinformatics experience.
Read more about Allen Yu

View More author details
Right arrow

Chapter 6. Embedding Matplotlib in Qt 5

There are several GUI libraries available, and one widely used library is Qt. In this book, we will be using Qt 5, the latest major version of this library. Unless explicitly mentioned, we are referring to Qt 5 when we simply state Qt throughout the chapter.

We will follow a similar progression to that in Chapter 5, Embedding Matplotlib in GTK+3, and we will present similar examples but this time written in Qt.

We believe that this method will allow us to directly compare the libraries, and it has the advantage of not leaving the How would I write something with library X? question unanswered.

In this chapter, we will learn how to:

  • Embed a Matplotlib figure into a Qt widget
  • Embed a figure and navigation toolbar into a Qt widget
  • Use events to update a Matplotlib plot in real time
  • Use QT Designer to draw a GUI and then use it with Matplotlib in a simple Python application

We will begin by giving an introduction to the library.

A brief introduction to Qt 5 and PyQt 5


Qt is a cross-platform application development framework widely used for graphical programs (GUI) and also for non-GUI tools.

Qt was developed by Trolltech (now owned by Nokia), and it's probably best known for being the foundation of the K Desktop Environment (KDE) for Linux.

The Qt toolkit is a collection of classes made to simplify the creation of programs. Qt is more than just a GUI toolkit. It includes components for abstractions of network sockets, threads, Unicode, regular expressions, SQL databases, SVG, OpenGL, and XML. It also has a fully functional web browser, help system, multimedia framework, and rich collection of GUI widgets.

Qt is available on several platforms, particularly Unix/Linux, Windows, macOS X, and also some embedded devices. As it uses native APIs of the platform to render the Qt controls, applications developed with Qt have a look and feel that fits the running environment (without looking like something alien in it).

Though...

Differences between Qt 4 and PyQt 4


PyQt is a comprehensive set of Python bindings for the Qt framework. However, PyQt 5 is not backward compatible with PyQt 4. It is noteworthy that PyQt 5 does not support any part of the Qt API that are marked as deprecated or obsolete in Qt v5.0. However, it is possible that some of these are included accidentally. If included, they are considered bugs and will be removed when found.

If you are familiar with Qt 4 or have read the first edition of this book, one thing to note is that the signals and slots of are no longer supported. Therefore, the following are not implemented in PyQt 5:

  • QtScript
  • QObject.connect()
  • QObject.emit()
  • SIGNAL()
  • SLOT()

Also, there is a modification in disconnect() as it no longer takes arguments and will disconnect all connections to the QObject instance when invoked.

However, new modules have been introduced, such as the following:

  • QtBluetooth
  • QtPositioning
  • Enginio

Let's start with a very simple example—calling a window. Again for best performance...

Summary


GUI design by using QT Creator/ QT Designer has enough material for a book on its own. Therefore, in this chapter, we aimed to show you just a glimpse of GUI design through PyQt 5. Upon finishing this chapter, the readers should now understand how to embed a figure in a QWidget, use the layout manager to pack a figure in a QWidget, create a timer, react to events and update a Matplotlib graph accordingly, and use QT Designer to draw a simple GUI for Matplotlib embedding.

We are now ready to learn another GUI library, wxWidgets.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Matplotlib for Python Developers. - Second Edition
Published in: Apr 2018Publisher: PacktISBN-13: 9781788625173
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (3)

author image
Aldrin Yim

Aldrin Yim is a PhD candidate and Markey Scholar in the Computation and System Biology program at Washington University, School of Medicine. His research focuses on applying big data analytics and machine learning approaches in studying neurological diseases and cancer. He is also the founding CEO of Codex Genetics Limited, which provides precision medicine solutions to patients and hospitals in Asia.
Read more about Aldrin Yim

author image
Claire Chung

Claire Chung is pursuing her PhD degree as a Bioinformatician at the Chinese University of Hong Kong. She enjoys using Python daily for work and lifehack. While passionate in science, her challenge-loving character motivates her to go beyond data analytics. She has participated in web development projects, as well as developed skills in graphic design and multilingual translation. She led the Campus Network Support Team in college, and shared her experience in data visualization in PyCon HK 2017.
Read more about Claire Chung

author image
Allen Yu

Allen Yu, PhD, is a Chevening Scholar, 2017-18, and an MSC student in computer science at the University of Oxford. He holds a PhD degree in Biochemistry from the Chinese University of Hong Kong, and he has used Python and Matplotlib extensively during his 10 years of bioinformatics experience.
Read more about Allen Yu