Reader small image

You're reading from  Python GUI Programming Cookbook. - Third Edition

Product typeBook
Published inOct 2019
Reading LevelIntermediate
Publisher
ISBN-139781838827540
Edition3rd Edition
Languages
Right arrow
Author (1)
Burkhard Meier
Burkhard Meier
author image
Burkhard Meier

Burkhard Meier is a professional software test automation designer, developer, and analyst. He has more than 17 years' professional experience working for several software companies in California, USA. He is the author of Python GUI Programming Cookbook, First and Second Edition. This book is also available as a Packt video course. He is also the author of the Python Projects Packt video course. In his professional career, he developed advanced in-house testing frameworks written in Python 3. He also developed advanced test automation GUIs in Python, which highly increased the productivity of the software development testing team. When not dreaming in Python code, he reads programming books about design, likes to go for long walks, and reads classical poetry.
Read more about Burkhard Meier

Right arrow

Matplotlib Charts

In this chapter, we will create beautiful charts that visually represent data. Depending on the format of the data source, we can plot one or more columns of data in the same chart.

We will be using the Python Matplotlib module to create our charts.

In a company I worked for, we had an existing program that collected data for analysis. It was a manual process to load the data into Excel and then generate charts within Excel.

I automated the entire process using Python and Matplotlib. With only one click of the mouse, the data got backed up to a network drive and, with another click, the charts got automatically created.

In order to create these graphical charts, we need to download additional Python modules, and there are several ways to install them.

This chapter will explain how to download the Matplotlib Python module along with all the other requisite Python...

Installing Matplotlib using pip with the .whl extension

The usual way to download additional Python modules is by using pip. The pip module comes pre-installed with the latest version of Python (3.7 and above).

If you are using an older version of Python, you may have to download both pip and setuptools by yourself.

This recipe will show how to successfully install Matplotlib using pip. We will be using the .whl extension for this installation, so this recipe will also show you how to install the wheel module.

Getting ready

First, let's find out whether you have the wheel module already installed. The wheel module is necessary to download and install Python packages that have the .whl extension.

We can find out what modules...

Creating our first chart

Now that we have all the required Python modules installed, we can create our own charts using Matplotlib.

We can create charts with only a few lines of Python code.

Getting ready

Successfully installing Matplotlib, as shown in the previous recipe, is a requirement for this recipe.

How to do it...

Using the minimum amount of code, we can create our first Matplotlib chart.

For the first chart, the steps are as follows:

  1. Create a new Python module and save it as Matplotlib_our_first_chart.py.
  2. Type the following code into the module:
import matplotlib...

Placing labels on charts

So far, we have used the default Matplotlib GUI. Now, we will create some tkinter GUIs from which we will be using Matplotlib.

This will require a few more lines of Python code and the importing of some more libraries, and it is well worth the effort, because we are gaining control of our paintings using canvases.

We will position labels onto both the horizontal and the vertical axes, that is, x and y. We will do this by creating a Matplotlib figure that we will draw on.

You will also learn how to use subplots, which will enable you to draw more than one graph in the same GUI window.

Getting ready

With the necessary Python modules installed and knowing where to find the official online documentation...

How to give the chart a legend

Once we start plotting more than one line of data points, things might become a little bit unclear. By adding a legend to our graphs, we can identify data, and tell what it actually means.

We do not have to choose different colors to represent the different data. Matplotlib automatically assigns a different color to each line of the data points.

All we have to do is create the chart and add a legend to it.

Getting ready

In this recipe, we will enhance the chart from the previous recipe, Placing labels on charts. We will only plot one chart.

How to do it...

...

Scaling charts

In the previous recipes, while creating our first charts and enhancing them, we hardcoded the scaling of how those values are visually represented.

While this served us well for the values we were using, we might have to plot charts from large databases.

Depending on the range of that data, our hardcoded values for the vertical y-dimension might not always be the best solution, and may make it hard to see the lines in our charts.

Getting ready

We will improve our code from the previous recipe, How to give the chart a legend. If you have not typed in all of the code from the previous recipes, just download the code for this chapter from the Packt website, and it will get you started (and then you can have a lot...

Adjusting the scale of charts dynamically

In the previous recipe, we learned how we can limit the scaling of our charts. In this recipe, we will go one step further by dynamically adjusting the scaling by both setting a limit and analyzing our data before we represent it.

Getting ready

We will enhance the code from the previous recipe, Scaling charts, by reading in the data we are plotting dynamically, averaging it, and then adjusting our chart.

While we would typically read in the data from an external source, in this recipe, we'll create the data we are plotting using Python lists, as can be seen in the code in the following section.

...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Python GUI Programming Cookbook. - Third Edition
Published in: Oct 2019Publisher: ISBN-13: 9781838827540
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

Author (1)

author image
Burkhard Meier

Burkhard Meier is a professional software test automation designer, developer, and analyst. He has more than 17 years' professional experience working for several software companies in California, USA. He is the author of Python GUI Programming Cookbook, First and Second Edition. This book is also available as a Packt video course. He is also the author of the Python Projects Packt video course. In his professional career, he developed advanced in-house testing frameworks written in Python 3. He also developed advanced test automation GUIs in Python, which highly increased the productivity of the software development testing team. When not dreaming in Python code, he reads programming books about design, likes to go for long walks, and reads classical poetry.
Read more about Burkhard Meier