How to Embed Matplotlib Graph in PyQt5

In this PyQt5 article iam going to show you How to Embed Matplotlib Graph, so

before starting our main topic if your interested in Python GUI Development with different

libraries , you can check the below links.

 

 

 

Also you can check more Python GUI articles in the below links

1: Kivy GUI Development Tutorials

2: Python TKinter GUI Development 

3: Psyide2 GUI Development 

4: wxPython GUI Development 

5: PyQt5 GUI Development Course

 

 

 

 

What is Matplotlib ? 

Matplotlib is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. Matplotlib can be used in Python scripts, the Python and IPython shells, the Jupyter notebook, web application servers, and four graphical user interface toolkits.

Matplotlib tries to make easy things easy and hard things possible. You can generate plots, histograms, power spectra, bar charts, errorcharts, scatterplots, etc., with just a few lines of code. For examples, see the sample plots and thumbnail gallery.

For simple plotting the pyplot module provides a MATLAB-like interface, particularly when combined with IPython. For the power user, you have full control of line styles, font properties, axes properties, etc, via an object oriented interface or via a set of functions familiar to MATLAB users.

 

 

 

Installation 

You can simple install matplotlib by using pip install matplotlib, or for more information about installation you can check the Installation Instruction.

 

 

 

 

What is PyQt5 ? 

Qt is set of cross-platform C++ libraries that implement high-level APIs for accessing many aspects of modern desktop and mobile systems. These include location and positioning services, multimedia, NFC and Bluetooth connectivity, a Chromium based web browser, as well as traditional UI development.

PyQt5 is a comprehensive set of Python bindings for Qt v5. It is implemented as more than 35 extension modules and enables Python to be used as an alternative application development language to C++ on all supported platforms including iOS and Android.

PyQt5 may also be embedded in C++ based applications to allow users of those applications to configure or enhance the functionality of those applications.

 

 

 

Installation 

The GPL version of PyQt5 can be installed from PyPI:

 

The wheels include a copy of the required parts of the LGPL version of Qt.

pip will also build and install the bindings from the sdist package but Qt’s qmake tool

must be on PATH.

The sip-install tool will also install the bindings from the sdist package but will allow

you to configure many aspects of the installation.

 

 

 

 

 

Now this is the complete code for How to Embed Matplotlib Graph in PyQt5.

 

 

 

 

So at the top we have imported the required libraries that we need, basically we need

PyQt5, Matplotlib and also Numpy.

 

 

 

 

OK this is our main window class that inherits from QMainWindow, and we have some

requirements of the window like title, width, height of the window, also we have called

our MyUI() method in this class.

 

 

 

 

 

So in this method we have created a Canvas with two QPushButton.

 

 

 

 

 

And this is our Canvas class that inherits from FigureCanvas.

 

 

 

 

 

Also in here we are going to plot a pie chart in PyQt5 window.

 

 

 

 

So in here every PyQt5 application must create an application object. The sys.argv 

parameter is a list of arguments from a command line.

 

 

 

 

Finally, we enter the mainloop of the application. The event handling starts from this point.

The mainloop receives events from the window system and dispatches them to the

application widgets.

 

 

 

 

Run the complete code and this will be the result

How to Embed Matplotlib Graph in PyQt5
How to Embed Matplotlib Graph in PyQt5

 

 

 

 

 

Also you can watch the complete video for this article

Subscribe and Get Free Video Courses & Articles in your Email

 

2 thoughts on “How to Embed Matplotlib Graph in PyQt5”

Comments are closed.

Share via
Copy link
Powered by Social Snap
×