Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Matplotlib for Python Developers. - Second Edition
Matplotlib for Python Developers. - Second Edition

Matplotlib for Python Developers.: Effective techniques for data visualization with Python, Second Edition

By Aldrin Yim , Claire Chung , Allen Yu
$15.99 per month
Book Apr 2018 300 pages 2nd Edition
eBook
$35.99 $24.99
Print
$43.99
Subscription
$15.99 Monthly
eBook
$35.99 $24.99
Print
$43.99
Subscription
$15.99 Monthly

What do you get with a Packt Subscription?

Free for first 7 days. $15.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details


Publication date : Apr 24, 2018
Length 300 pages
Edition : 2nd Edition
Language : English
ISBN-13 : 9781788625173
Category :
Table of content icon View table of contents Preview book icon Preview Book

Matplotlib for Python Developers. - Second Edition

Chapter 1. Introduction to Matplotlib

"A picture is worth a thousand words." - Fred R Barnard

Welcome aboard to the journey of creating good data visuals. In this era of exploding big data, we are probably well aware of the importance of data analytics. Developers are keen to join the data mining game, and build tools to collect and model all kinds of data. Even for non-data analysts, information such as performance test results and user feedback is often paramount in improving the software being developed. While strong statistical skills surely set the foundation of successful software development and data analysis, good storytelling is crucial even for the best data crunching results. The quality of graphical data representation often determines whether or not you can extract useful information during exploratory data analysis and get the message across during the presentation.

Matplotlib is a versatile and robust Python plotting package; it provides clean and easy ways to produce various quality data graphics and offers huge flexibility for customization.

In this chapter, we will introduce Matplotlib as follows: what it does, why you would want to use it, and how to get started. Here are the topics we will cover:

  • What is Matplotlib?
  • Merits of Matplotlib
  • What's new in Matplotlib?
  • Matplotlib websites and online documentation.
  • Output formats and backends.
  • Setting up Matplotlib.

What is Matplotlib?


Matplotlib is a Python package for data visualization. It allows easy creation of various plots, including line, scattered, bar, box, and radial plots, with high flexibility for refined styling and customized annotation. The versatile artist module allows developers to define basically any kind of visualization. For regular usage, Matplotlib offers a simplistic object-oriented interface, the pyplot module, for easy plotting.

Besides generating static graphics, Matplotlib also supports an interactive interface which not only aids in creating a wide variety of plots but is also very useful in creating web-based applications.

Matplotlib is readily integrated into popular development environments, such as Jupyter Notebook, and it supports many more advanced data visualization packages.

Merits of Matplotlib

There are many advantages in creating data visualization with code so that the visualization streamlines into part of the result generation pipeline. Let's have a look at some of the key advantages of the Matplotlib library. 

Easy to use

The Matplotlib plotting library is easy to use in several ways:

  • Firstly, the object-oriented module structures simplify the plotting process. More often than not, we're only required to call import maplotlib.pyplot as plt to import the plotting API to create and customize many basic plots.
  • Matplotlib is highly integrated with two common data analytics packages, pandas and NumPy. For example, we can simply append .plot() to a pandas DataFrame such as by df.plot() to create a simple plot, and customize its styling with Matplotlib syntax.
  • For styling, Matplotlib offers functions to alter the appearance of each feature, and ready-made default style sheets are also available to avoid these extra steps when refined aesthetics is not required.

Diverse plot types

Often in data analytics, we need sophisticated plots to express our data. Matplotlib offers numerous plotting APIs natively, and is also the basis for a collection of third-party packages for additional functionalities, including:

  • Seaborn: Provides simple plotting APIs, including some advanced plot types, with aesthetically appealing default styling
  • HoloViews: Creates interactive plots with metadata annotation from bundled data 
  • Basemap/GeoPandas/Canopy: Maps data values to colors on geographical maps

We would learn some of the applications of these third-party packages in later chapters on advanced plotting.

Hackable to the core (only when you want)

When we want to go beyond the default settings to ensure that the resultant figure meets our specific purpose, we can customize the appearance and behaviors of each plot feature:

  • Per-element styling is possible
  • The ability to plot data values as colors and draw any shape of patches allows the creation of almost any kind of visualization
  • Useful in customizing plots created by extensions such as Seaborn

Open source and community support

As Matplotlib is open source, it enables developers and data analysts to use it for free. The users also have the freedom to improve and contribute to the Matplotlib library. As part of the open source experience, the users get prompt online support from the members of the global community on various platforms and forums.

What's new in Matplotlib 2.x?

Matplotlib supports Python 3 since version 1.2, released in 2013. The Matplotlib 2.0 release introduced a number of changes and upgrades to improve data visualization projects. Let us look at some of the key improvements and upgrades. 

Improved functionality and performance

Matplotlib 2.0 presents new features that improve user experience, including speed, and output quality, as well as resource usage.

Improved color conversion API and RGBA support

The alpha channel that specifies the transparency level is fully supported in Matplotlib 2.0.

Improved image support

Matplotlib 2.0 now resamples images with less memory and less data type conversion.

Faster text rendering

Community developers claim that the speed of text rendering by the Agg backend has improved by 20%.

Change in the default animation codec

A very efficient codec, H.264, is now used as the default, which replaces MPEG-4, to generate video output for animated plots. With H.264, we can now have longer video record time and lesser data traffic and loading time thanks to the higher compression rate and smaller output file size. It is also noted that real-time playback of H.264 videos is better than those encoded in MPEG-4.

Changes in default styles

There are a number of style changes for improved visualization, such as more intuitive colors by default. We will discuss more in the chapter on figure aesthetics.

For details on all Matplotlib updates, you may visit http://matplotlib.org/devdocs/users/whats_new.html.

Matplotlib website and online documentation

As developers, you probably recognize the importance of reading documentation and manuals to get acquainted with syntax and functionality. We would like to reiterate the importance of reading the library documentation and encourage you to do the same. You can find the documentation here: https://matplotlib.org. On the official Matplotlib website, you would find the documentation for each function, news of latest releases and ongoing development, and a list of third-party packages, as well as tutorials and galleries of example plots.

However, building advanced and sophisticated plots by reading through documentation from scratch means a much steeper learning curve and a lot more time spent, especially when the documentation is regularly updated for better comprehension. This book aims to provide the reader with a guided road-map to accelerate the learning process, save time and effort, and put theory into practice. The online manuals can serve as the atlases you can turn to whenever you want to explore further.

The Matplotlib source code is available on GitHub at https://github.com/matplotlib/matplotlib. We encourage our readers to fork it and add their ideas!

Output formats and backends 

Matplotlib enables users to obtain output plots as static figures. The plots can also be piped and made responsive through interactive backends. 

Static output formats

Static images are the most commonly used output format for reporting and presentation purposes, and for our own quick inspection of data. Static images can be classified into two distinct categories.

Raster images

Raster is the classic image format that provides support to a wide variety of image files, including PNG, JPG and BMP. Each raster image can be seen as a dense array of color values. For raster images, resolution matters.

The amount of image details kept is measured in dots per inch (DPI). The higher the DPI value (that is, the more pixel dots kept in it), the clearer the resultant image would be, even when stretched to a larger size. Of course, the file size and computational resources needed for the rendering would increase accordingly.

Vector images

For vector images, instead of a matrix of discrete color dots, information is saved as paths, which are lines joining dots. They scale without losing any details:

  • SVG
  • PDF
  • PS

Setting up Matplotlib


Now that we have a comprehensive overview of the capabilities and functionalities of Matplotlib, we are ready to get our hands dirty and work through some examples. We will begin after ensuring that we have set up the Matplotlib environment. Follow along the steps discussed to set up the environment. 

Installing Python

Since version 2.0, Matplotlib supports both Python 2.7 and 3.4+. We are using Python 3 in this book, which is the latest stable Python version. You can download Python from http://www.python.org/download/.

Python installation for Windows 

Python comes as an installer or zipped source code for Windows. We recommend the executable installer. Choose the right computer architecture for the best performance. You can call Python in the Command Prompt by pressing the Windows + R keys and typing cmd.exe, as shown in the following screenshot:

Python installation for macOS

macOS natively comes with Python 2.7. To install Python 3.4+, download the installation wizard and follow the instructions. Following is a screenshot of the wizard at the first step:

Note

Some Python packages require Xcode command-line tools to compile properly. Xcode can be obtained from the Mac App Store. To install the command-line tools, enter the following command in the Terminal: xcode-select --install. Then follow the installation prompts.

Python installation for Linux

Most Linux distributions have Python 3.4 preinstalled. You may confirm this by typing python3 in the Terminal. If you see the following, it means Python 3.4 is present:

Python3.6.3 (default, Oct 62017, 08:44:35) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license"for more information. >>>​

If the Python shell does not appear at the command, you can install Python 3 with apt, the Linux software management tool:

sudo apt update
sudo apt install Python3 build-essential

The build-essential package contains compilers that are useful for building non-pure Python packages. Meanwhile, you may need to substitute apt with apt-get if you have Ubuntu 14.04 or older.

Installing Matplotlib

Matplotlib requires a large number of dependencies. We recommend installing Matplotlib by a Python package manager, which will help you to automatically resolve and install dependencies upon each installation or upgrade of a package. We will demonstrate how to install Matplotlib with pip.

About the dependencies

Matplotlib depends on a host of Python packages for background calculation, graphic rendering, interaction, and more. They are NumPy, libpng, and FreeType, and so on. Depending on the usage, users can install additional backend packages, such as PyQt5, for a better user interface.

Installing the pip Python package manager

We recommend installing Matplotlib using the Python package manager pip; it resolves basic dependencies automatically. pip is installed with Python 2 >= 2.7.9 or Python 3 >= 3.4 binaries.

If pip is not installed, you may do so by downloading get-pip.py from http://bootstrap.pypa.io/get-pip.py, and running it in the console:

python3 get-pip.py

To upgrade pip to the latest version, do this: 

pip3 install --upgrade pip

The documentation for pip can be found at http://pip.pypa.io.

Installing Matplotlib with pip

Enter python3 -m pip install matplotlib on the Terminal/Command Prompt to install. Add a --user option for pip install for users without root/admin rights where necessary.

Setting up Jupyter Notebook

To create our plots, we need a user-friendly development environment.

Jupyter Notebook provides an interactive coding ground to edit and run your code, display the results, and document them neatly. Data and methods can be loaded to the memory for reuse within a session. As each notebook is hosted as a web server, you can connect to notebook instances running at a remote server on a browser.

If you are excited to try it out before installing, you may go to https://try.jupyter.org and open a Python 3 notebook.

To install Jupyter, type this in your console: 

python3 -m pip install jupyter

Starting a Jupyter Notebook session

Just type jupyter notebook in the console. This will start a Jupyter Notebook session as a web server.

By default, a notebook session should pop up on your default browser. To manually open the page, type localhost:8888 as the URL. Then you will enter the following home page of the Jupyter Notebook:

You can choose to host the notebook on different ports, for instance, when you are running multiple notebooks. You can specify the port to use with the --port=<customportnum> option.

Since the release of 4.3, token authentication has been added to Jupyter, so you may be asked for a token password before entering the notebook home page, as shown in the following screenshot:

To retrieve the token, such as when visiting the running notebook from a different browser or machine, you may call jupyter notebook list from the console:

Running Jupyter Notebook on a remote server

To open a notebook running on a remote server, you may set up port forwarding during SSH, as follows:

ssh –L 8888:localhost:8888 mary@remoteserver

Then you may open the notebook again with localhost:8888 as the URL.

Note

When multiple users are running Jupyter Notebooks on the same server on the same port (say the default, 8888) and each uses the same port forwarding, there is a possibility that your notebook content will be forwarded to another user who cannot read his/her own content without changing the port. While this might be fixed with later releases, it is recommended to change the port from the default. 

To upgrade from a previous version, run the following command:

pip3 install --upgrade matplotlib

pip will automatically collect and install the Matplotlib dependencies for you.

Editing and running code

A Jupyter Notebook has boxes called cells. It begins with the text input area for code editing, known as the gray box cell, by default. To insert and edit the code do the following:

  1. Click inside the gray box.
  2. Type your Python code inside it.
  3. Click on the play button or press Shift + Enter to run the current cell and move the cursor to the next cell:

Once a cell is run, the relevant data and methods are loaded to the memory and can be used across cells in the same notebook kernel. No reloading is needed unless for an intended change. This saves effort and time in debugging and reloading large datasets.

Manipulating notebook kernel and cells

You can use the toolbar at the top to manipulate cells and the kernel. The available functions are annotated as follows:

Verify your output amount before running a cell! A huge output stream doesn't usually kill your console, but it can easily crash your browser and notebook in a matter of seconds. This issue has been addressed since Jupyter 4.2 by stopping large output. However, it does not guarantee to capture all non-stop output. Therefore, readers are advised to exercise caution and avoid attempts to obtain large output results in a notebook cell. Consider slicing it for a glimpse or obtaining output in another file:

Embed your Matplotlib plots

Matplotlib is highly integrated into Jupyter Notebook. Use the Jupyter built-in magic command %matplotlib inline (set as default in the current release) to display resultant plots as static image output at each cell:

Alternatively, you can run a magic cell command—%matplotlib notebook to use the interactive Matplotlib GUI for zooming or rotating in the same output area:

Documenting in Markdown

Jupyter Notebook supports Markdown syntax for organized documentation:

  1. Select Markdown from the drop-down list in the toolbar.
  2. Write your notes in the gray input box.
  3. Click on Run or Shift + Enter:

After running the cell, the text will be styled in the display:

You can find a detailed Markdown cheat sheet by Adam Pritchard at https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet.

Save your hard work!

Jupyter Notebook auto-saves itself every 2 minutes. As good practice, you should save it yourself more often by clicking on the floppy icon on the toolbar, or more conveniently by Ctrl + S.

Each project you open on Jupyter is saved as the JSON-based .ipynb notebook format:

The .ipynb notebook is portable across different Jupyter servers. Notebooks can be exported as basic runnable Python script .py, Markdown .md for documentation, and web page format .html for instant display of the flow of your project without having the readers install Jupyter Notebook in advance. It also supports LaTex format and PDF conversion via LaTex upon installation of the dependency Pandoc. If interested, you may check out the installation instructions at http://pandoc.org/installing.html.

Summary


Woo-hoo! We've taken our first steps in our Matplotlib journey together. You can rest assured you have a comprehensive understanding of Matplotlib's capabilities and also have the requisite environment set up. Now that we have successfully ventured into data visualization and gotten our feet wet, let's go ahead and build our first plots!

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Perform effective data visualization with Matplotlib and get actionable insights from your data
  • Design attractive graphs, charts, and 2D plots, and deploy them to the web
  • Get the most out of Matplotlib in this practical guide with updated code and examples

Description

Python is a general-purpose programming language increasingly being used for data analysis and visualization. Matplotlib is a popular data visualization package in Python used to design effective plots and graphs. This is a practical, hands-on resource to help you visualize data with Python using the Matplotlib library. Matplotlib for Python Developers, Second Edition shows you how to create attractive graphs, charts, and plots using Matplotlib. You will also get a quick introduction to third-party packages, Seaborn, Pandas, Basemap, and Geopandas, and learn how to use them with Matplotlib. After that, you’ll embed and customize your plots in third-party tools such as GTK+3, Qt 5, and wxWidgets. You’ll also be able to tweak the look and feel of your visualization with the help of practical examples provided in this book. Further on, you’ll explore Matplotlib 2.1.x on the web, from a cloud-based platform using third-party packages such as Django. Finally, you will integrate interactive, real-time visualization techniques into your current workflow with the help of practical real-world examples. By the end of this book, you’ll be thoroughly comfortable with using the popular Python data visualization library Matplotlib 2.1.x and leveraging its power to build attractive, insightful, and powerful visualizations.

What you will learn

Create 2D and 3D static plots such as bar charts, heat maps, and scatter plots Get acquainted with GTK+3, Qt5, and wxWidgets to understand the UI backend of Matplotlib Develop advanced static plots with third-party packages such as Pandas, GeoPandas, and Seaborn Create interactive plots with real-time updates Develop web-based, Matplotlib-powered graph visualizations with third-party packages such as Django Write data visualization code that is readily expandable on the cloud platform

What do you get with a Packt Subscription?

Free for first 7 days. $15.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details


Publication date : Apr 24, 2018
Length 300 pages
Edition : 2nd Edition
Language : English
ISBN-13 : 9781788625173
Category :

Table of Contents

16 Chapters
Title Page Chevron down icon Chevron up icon
Dedication Chevron down icon Chevron up icon
Packt Upsell Chevron down icon Chevron up icon
Contributors Chevron down icon Chevron up icon
Preface Chevron down icon Chevron up icon
Introduction to Matplotlib Chevron down icon Chevron up icon
Getting Started with Matplotlib Chevron down icon Chevron up icon
Decorating Graphs with Plot Styles and Types Chevron down icon Chevron up icon
Advanced Matplotlib Chevron down icon Chevron up icon
Embedding Matplotlib in GTK+3 Chevron down icon Chevron up icon
Embedding Matplotlib in Qt 5 Chevron down icon Chevron up icon
Embedding Matplotlib in wxWidgets Using wxPython Chevron down icon Chevron up icon
Integrating Matplotlib with Web Applications Chevron down icon Chevron up icon
Matplotlib in the Real World Chevron down icon Chevron up icon
Integrating Data Visualization into the Workflow Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.