40,81 €
Build attractive, insightful, and powerful visualizations to gain quality insights from your data
Key Features
Book Description
Matplotlib provides a large library of customizable plots, along with a comprehensive set of backends. Matplotlib 3.0 Cookbook is your hands-on guide to exploring the world of Matplotlib, and covers the most effective plotting packages for Python 3.7.
With the help of this cookbook, you'll be able to tackle any problem you might come across while designing attractive, insightful data visualizations. With the help of over 150 recipes, you'll learn how to develop plots related to business intelligence, data science, and engineering disciplines with highly detailed visualizations. Once you've familiarized yourself with the fundamentals, you'll move on to developing professional dashboards with a wide variety of graphs and sophisticated grid layouts in 2D and 3D. You'll annotate and add rich text to the plots, enabling the creation of a business storyline. In addition to this, you'll learn how to save figures and animations in various formats for downstream deployment, followed by extending the functionality offered by various internal and third-party toolkits, such as axisartist, axes_grid, Cartopy, and Seaborn.
By the end of this book, you'll be able to create high-quality customized plots and deploy them on the web and on supported GUI applications such as Tkinter, Qt 5, and wxPython by implementing real-world use cases and examples.
What you will learn
Who this book is for
The Matplotlib 3.0 Cookbook is for you if you are a data analyst, data scientist, or Python developer looking for quick recipes for a multitude of visualizations. This book is also for those who want to build variations of interactive visualizations.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 558
Veröffentlichungsjahr: 2018
Copyright © 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Sunith ShettyAcquisition Editor: Namrata PatilContent Development Editor: Unnati GuhaTechnical Editor: Sayli NikaljeCopy Editor: Safis EditingProject Coordinator: Manthan PatelProofreader: Safis EditingIndexer: Rekha NairGraphics: Jisha ChirayilProduction Coordinator: Arvindkumar Gupta
First published: October 2018
Production reference: 1191018
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-78913-571-8
www.packtpub.com
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Srinivasa Rao Poladi has been in the IT services industry for over two decades, providing consulting and implementation services in data warehousing, business intelligence, and machine learning areas for global customers.
He has worked with Wipro Technologies for two decades and played key leadership roles in building large technology practices and growing them to multi-million $ business.
He spoke at international conferences, published many blogs and white papers in the areas of big data, business intelligence, and analytics.
He is a co-founder of krtrimaIQ a consulting firm that provides cognitive solutions to create tomorrow's Intelligent Enterprises powered by automation, big data, machine learning, and deep learning.
Nikhil Borkar holds a CQF designation and a post-graduate degree in quantitative finance. He also holds the Certified Financial Crime Examiner and Certified Anti-Money Laundering Professional qualifications. He is a registered research analyst with the Securities and Exchange Board of India (SEBI) and has a keen grasp of the Indian regulatory landscape pertaining to securities and investments. He is currently working as an independent FinTech and legal consultant. Prior to this, he worked with Morgan Stanley Capital International (MSCI) as a Global RFP Project Manager.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Matplotlib 3.0 Cookbook
Packt Upsell
Why subscribe?
Packt.com
Contributors
About the author
About the reviewer
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Sections
Getting ready
How to do it…
How it works…
There's more…
See also
Get in touch
Reviews
Anatomy of Matplotlib
Introduction
Architecture of Matplotlib
Backend layer
Artist layer
Scripting layer
Elements of a figure
Figure
Axes
Axis
Label
Legend
Title
Ticklabels
Spines
Grid
Working in interactive mode
Getting ready
How to do it...
How it works...
There's more...
Working in non-interactive mode
How to do it...
How it works...
Reading from external files and plotting
Getting ready
How to do it...
Reading from a .txt file
Reading from a .csv file
Reading from an .xlsx file
Plotting the graph
How it works...
Changing and resetting default environment variables 
Getting ready
How to do it...
How it works...
There's more...
Getting Started with Basic Plots
Introduction
Line plot
Getting ready
How to do it...
How it works...
There's more...
Bar plot
Getting ready
How to do it...
How it works...
There's more...
Scatter plot
Getting ready
How to do it...
How it works...
There's more...
Bubble plot
Getting ready
How to do it...
How it works...
Stacked plot
Getting ready
How to do it...
How it works...
Pie plot
Getting ready
How to do it...
How it works...
Table chart
Getting ready
How to do it...
How it works...
Polar plot
Getting ready
How to do it...
How it works...
There's more...
Histogram
Getting ready
How to do it...
How it works...
There's more...
Box plot
Getting ready
How to do it...
How it works...
There's more...
Violin plot
Getting ready
How to do it...
How it works...
Reading and displaying images
Getting ready
How to do it...
How it works...
Heatmap
Getting ready
How to do it...
How it works...
Hinton diagram
Getting ready
How to do it...
How it works...
Contour plot
Getting ready
How to do it...
How it works...
There's more...
Triangulations
Getting ready
How to do it...
How it works...
There's more...
Stream plot
Getting ready
How to do it...
How it works...
There's more...
Path
Getting ready
How to do it...
How it works...
Plotting Multiple Charts, Subplots, and Figures
Introduction
Plotting multiple graphs on the same axes
Getting ready
How to do it...
How it works...
Plotting subplots on the same figure
Getting ready
How to do it...
How it works...
There's more...
Plotting multiple figures in a session
Getting ready
How to do it...
How it works...
There's more...
Logarithmic scale
Getting ready
How to do it...
How it works...
There's more...
Using units of measurement
Getting ready
How to do it...
How it works...
There's more...
Developing Visualizations for Publishing Quality
Introduction
Color, line style, and marker customization
Getting ready
How to do it...
How it works...
Working with standard colormaps
Getting ready
How to do it...
How it works...
There's more...
User-defined colors and colormaps
Getting ready
How to do it...
How it works...
There's more...
Working with legend
Getting ready
How to do it...
How it works...
There's more...
Customizing labels and titles
Getting ready
How to do it...
How it works...
There's more...
Using autoscale and axis limits
Getting ready
How to do it...
How it works...
Customizing ticks and ticklabels
Getting ready
How to do it...
How it works...
There's more...
Customizing spines
Getting ready
How to do it...
How it works...
Twin axes
Getting ready
How to do it...
How it works...
There's more...
Using hatch
Getting ready
How to do it...
How it works...
Using annotation
Getting ready
How to do it...
How it works...
Using style sheets
Getting ready
How to do it...
How it works...
There's more...
Plotting with Object-Oriented API
Introduction
Plotting a correlation matrix using pyplot and object-oriented APIs
Getting ready
How to do it...
How it works...
Plotting patches using object-oriented API
Getting ready
How to do it...
How it works...
Plotting collections using object-oriented API
Getting ready
How to do it...
How it works...
Plotting with Advanced Features
Using property cycler
Getting ready
How to do it...
How it works...
There's more...
Using Path effects
Getting ready
How to do it...
How it works...
There's more...
Using transforms
Transforming data co-ordinates to display co-ordinates
Getting ready
How to do it...
How it works...
There's more...
Using axes and blended co-ordinate system transforms
Getting ready
How to do it...
How it works...
Taking control of axes positions
Getting ready
How to do it...
How it works...
GridSpec for figure layout
Using GridSpec
Getting ready
How to do it...
How it works...
There's more...
GridSpec alignment
Getting ready
How to do it...
How it works ...
Constrained layout
Getting ready
How to do it...
How it works...
Using GridSpecFromSubplotSpec
Getting ready
How to do it...
How it works...
Using origin and extent for image orientation
Getting ready
How to do it...
How it works...
Geographical plotting using geopandas
Getting ready
How to do it...
How it works...
Embedding Text and Expressions
Introduction
Using mathematical expressions with a font dictionary
Getting ready
How to do it...
How it works...
Annotating a point on a polar plot
Getting ready
How to do it...
How it works...
Using ConnectionPatch
Getting ready
How to do it...
How it works...
Using a text box
Getting ready
How to do it...
How it works...
There's more...
Plotting area under an integral curve
Getting ready
How to do it...
How it works...
Defining custom markers
Getting ready
How to do it...
How it works...
Fractions, regular mathematical expressions, and symbols
Getting ready
How to do it...
How it works...
There's more...
Word embeddings in two dimensions
Getting ready
How to do it...
How it works...
Saving the Figure in Different Formats
Introduction
Saving the figure in various formats
Getting ready
How to do it...
How it works...
There's more...
Avoiding truncation while saving the figure
Getting ready
How to do it...
How it works...
Saving partial figures
Getting ready
How to do it...
How it works...
Managing image resolution
Getting ready
How to do it...
How it works...
Managing transparency for web applications
Getting ready
How to do it...
How it works...
Creating multi-page PDF reports
Getting ready
How to do it...
How it works...
Developing Interactive Plots
Introduction
Events and callbacks
Exception handling
Getting ready
How to do it...
How it works...
There's more...
Key press and release events
Getting ready
How to do it...
How it works...
Mouse button press event
Getting ready
How to do it...
How it works...
Motion notify and mouse button press events
Getting ready
How to do it...
How it works...
Pick event
Getting ready
How to do it...
How it works...
Figure and axes, enter and leave events
Getting ready
How to do it...
How it works...
Using twin axes for plotting four temperature scales
Getting ready
How to do it...
How it works...
Widgets
Cursor
Getting ready
How to do it...
How it works...
Buttons
Getting ready
How to do it...
How it works...
Check buttons
Getting ready
How to do it...
How it works...
Radio buttons
Getting ready
How to do it...
How it works...
Textbox
Getting ready
How to do it...
How it works...
Animation
Animated sigmoid curve
Getting ready
How to do it...
How it works...
Saving the animation to an mp4 file
Getting ready
How to do it...
How it works...
Exponentially decaying tan function
Getting ready
How to do it...
How it works...
Animated bubble plot 
Getting ready
How to do it...
How it works...
Animating multiple line plots
Getting ready
How to do it...
How it works...
Animation of images
Getting ready
How to do it...
How it works...
Embedding Plots in a Graphical User Interface
Introduction
Interface between the Matplotlib and GUI applications
Using the Slider and Button Widgets of Matplotlib
Getting ready
How to do it...
How it works...
Using the Slider and Button widgets of Tkinter GUI 
Getting ready
How to do it...
How it works...
Embedding Matplotlib in a Tkinter GUI application
Getting ready
How to do it...
How it works...
Using the Slider and Button widgets of WxPython GUI
Getting ready
How to do it...
How it works...
Embedding Matplotlib in to a wxPython GUI application
Getting ready
How to do it...
How it works...
Using the Slider and Button widgets of Qt's GUI
Getting ready
How to do it...
How it works...
Embedding Matplotlib in to a Qt GUI application
Getting ready
How to do it...
How it works...
Plotting 3D Graphs Using the mplot3d Toolkit
Introduction
Line plot
Getting ready
How to do it...
How it works...
Scatter plot
Getting ready
How to do it...
How it works...
Bar plot
Getting ready
How to do it...
How it works...
Polygon plot
Getting ready
How to do it...
How it works...
There's more...
Contour plot
Getting ready
How to do it...
How it works...
There's more...
Surface plot
Getting ready
How to do it...
How it works...
Wireframe plot
Getting ready
How to do it...
How it works...
Triangular surface plot
Getting ready
How to do it...
How it works...
Plotting 2D data in 3D
Getting ready
How to do it...
How it works...
3D visualization of linearly non-separable data in 2D
Getting ready
How to do it...
How it works...
Word embeddings
Getting ready
How to do it...
How it works...
Using the axisartist Toolkit
Introduction
Understanding attributes in axisartist
Getting ready
How to do it...
How it works...
Defining curvilinear grids in rectangular boxes
Getting ready
How to do it...
How it works...
Defining polar axes in rectangular boxes
Getting ready
How to do it...
How it works...
Using floating axes for a rectangular plot
Getting ready
How to do it...
How it works...
Creating polar axes using floating axes
Getting ready
How to do it...
How it works...
Plotting planetary system data on floating polar axes
Getting ready
How to do it...
How it works...
Using the axes_grid1 Toolkit
Introduction
Plotting twin axes using the axisartist and axesgrid1 toolkits
Getting ready
How to do it...
How it works...
There's more...
Using AxesDivider to plot a scatter plot and associated histograms
Getting ready
How to do it...
How it works...
Using AxesDivider to plot a colorbar
Getting ready
How to do it...
How it works...
Using ImageGrid to plot images with a colorbar in a grid
Getting ready
How to do it...
How it works...
Using inset_locator to zoom in on an image
Getting ready
How to do it...
How it works...
Using inset_locator to plot inset axes
Getting ready
How to do it...
How it works...
Plotting Geographical Maps Using Cartopy Toolkit
Introduction
Plotting basic map features
Getting ready
How to do it...
How it works...
Plotting projections
Getting ready
How to do it...
How it works...
Using grid lines and labels
Getting ready
How to do it...
How it works...
Plotting locations on the map
Getting ready
How to do it...
How it works...
Plotting country maps with political boundaries
Getting ready
How to do it...
How it works...
Plotting country maps using GeoPandas and cartopy
Getting ready
How to do it...
How it works...
Plotting populated places of the world
Getting ready
How to do it...
How it works...
Plotting the top five and bottom five populated countries
Getting ready
How to do it...
How it works...
Plotting temperatures across the globe
Getting ready
How to do it...
How it works...
Plotting time zones
Getting ready
How to do it...
How it works...
Plotting an animated map
Getting ready
How to do it...
How it works...
Exploratory Data Analysis Using the Seaborn Toolkit
Introduction
Snacks Sales dataset
Wine Quality
Semantic and facet variables
Relational plots
Line plots with one-to-one and one-to-many relationships
Getting ready
How to do it...
How it works...
There's more...
Line plots with a long-form dataset
Getting ready
How to do it...
How it works...
Scatter plots
Getting ready
How to do it...
How it works...
There's more...
Categorical plots
Strip and swarm plots
Getting ready
How to do it...
How it works...
Box and boxn plots
Getting ready
How to do it...
How it works...
Bar and count plots
Getting ready
How to do it...
How it works...
Violin plots
Getting ready
How to do it...
How it works...
Point plots
Getting ready
How to do it...
How it works...
Distribution plots
distplot()
Getting ready
How to do it...
How it works...
kdeplot()
Getting ready
How to do it...
How it works...
Regression plots
regplot() and residplot()
Getting ready
How to do it...
How it works...
lmplot()
Getting ready
How to do it...
How it works...
Multi-plot grids
jointplot() and JointGrid()
Getting ready
jointplot()
How to do it...
How it works...
JointGrid()
How to do it...
How it works...
pairplot() and PairGrid()
Getting ready
pairplot()
How to do it...
How it works...
PairGrid()
How to do it...
How it works...
FacetGrid()
Getting ready
How to do it...
How it works...
Matrix plots
Heatmaps
Getting ready
How to do it...
How it works...
Clustermaps
Getting ready
How to do it...
How it works...
Other Books You May Enjoy
Leave a review - let other readers know what you think
In the era of big data, finding valuable business insights is akin to finding a needle in a haystack. Visualization plays a critical role in finding those nuggets from an ever-increasing volume and variety of data. Matplotlib, with its rich visualization functionality, makes the process of exploratory data analysis user friendly and more productive.
Matplotlib's core functionality is vast, and it is further enhanced by many in-house and third-party toolkits. Many of the books on the market cover only a small portion of its complete functionality. In this book, we have covered Matplotlib's complete core functionality and many of its popular toolkits.
Matplotlib is popular among machine learning practitioners and researchers who use the Python ecosystem. With its rich functionality, it can be used in business intelligence and operational reporting applications. In this book, we have made an attempt to present examples from these applications.
While a recipe-based cookbook approach makes this book a reference guide for quick solutions, we have covered sufficient theoretical background to make it easy for beginners as well.
This book is for data analysts, business analysts, data scientists, and Python developers who are looking for quick solutions for a wide variety of visualization applications, such as ad hoc reports, professional dashboards, exploratory data analysis, interactive analysis, embedded visualizations in selected GUI toolkits and web applications, three-dimensional plots, and geographical maps.
Those who are interested in developing business intelligence, machine learning, scientific, or engineering applications will also benefit from the recipes that are relevant for each of these disciplines.
Chapter 1, Anatomy of Matplotlib, explains the architecture of Matplotlib, various elements of a figure, interactive and non-interactive modes of operation, and how to customize environmental parameters.
Chapter 2, Getting Started with Basic Plots, introduces many types of graph that are commonly used in business intelligence and machine learning applications, including line, scatter, bar, stacked, histogram, box, violin, contour plots, heatmaps, and Hinton diagrams.
Chapter 3, Plotting Multiple Graphs, Subplots, and Figures, shows how to organize graphs into subplots and figures.
Chapter 4, Developing Visualizations for Publishing Quality, illustrates how to customize various attributes of a figure, including color, fonts, labels, titles, legend, spines, styles, markers, and annotation.
Chapter 5, Plotting with the Object-Oriented API, introduces the object-oriented API and compares it with the pyplot API. The object-oriented API gives flexibility in designing complex dashboards as required, but requires Python programming experience if you want to write code. The pyplot API comes with pre-packaged graphs that require simple commands to plot, without needing to write much Python code.
Chapter 6, Plotting with Advanced Features, covers how to develop complex visualization applications by using the advanced customization of legends, artist, and layout, as well as cycling object properties, origin and extent in images, transforms, animations, event handling, and path effects.
Chapter 7, Embedding Text and Expressions, covers how to add text to plots with regular text, annotations and mathematical expressions.
Chapter 8, Saving the Figure in Different Formats, explains how to save figures to external output files in PNG, PDF, SVG, and PS formats.
Chapter 9, Developing Interactive Plots, explains how to develop interactive plots using event handling, animations, and widgets. These features enable the users to perform interactive analysis.
Chapter 10, Embedding Plots in Graphical User Interface, explains how to embed Matplotlib plots into other graphical user interfaces used for developing applications.
Chapter 11, Plotting 3D Graphs Using the mplot3d Toolkit, covers how to use the mplot3D toolkit to plot 3D graphs, and the next two chapters cover two more toolkits.
Chapter 12, Using the axisartist Toolkit, explains that while the standard Matplotlib axes uses a traditional Cartesian coordinate system, it can't handle special features such as curved or floating axes that are useful in plotting geographical or planetary systems. This chapter explains how to create special applications using the axisartist toolkit.
Chapter 13, Using the axes_grid1 Toolkit, covers the axes_grid1 toolkit. This toolkit enables you to plot images in a grid with an associated color bar that aligns well with the image and also enables anchor images as legends, zoom in/out effects, and more.
Chapter 14, Plotting Geographical Maps Using the Cartopy Toolkit, explains wide variety of features that cater to many different user communities. We will cover most of the features typically used in business applications.
Chapter 15, Exploratory Data Analysis Using the Seaborn Toolkit, explains the process of exploratory data analysis using exhaustive features of seaborn toolkit.
Basic knowledge of Python is enough to understand the content in this book, except for Chapters 9, Developing Interactive Plots and Chapter 10, Embedding Plots in a Graphical User Interface. These two chapters deal with interactive plotting and embedded applications that need medium-level Python programming experience.
Many Python distributions automatically include Matplotlib, along with all its dependencies. If you have not installed any standard Python distributions, you can follow the installation process at https://matplotlib.org/users/installing.html to install Matplotlib and its associated dependencies.
You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packt.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at
www.packt.com
.
Select the
SUPPORT
tab.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR/7-Zip for Windows
Zipeg/iZip/UnRarX for Mac
7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Matplotlib-3.0-Cookbook. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/9781789135718_ColorImages.pdf.
There are a number of text conventions used throughout this book.
CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "We will follow the order of .txt, .csv, and .xlsx files, in three separate sections."
A block of code is set as follows:
import matplotlib.pyplot as pltimport pandas as pdimport numpy as npfrom matplotlib import cm
Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "When you run the program and click Next and Next, you will see the following three figures, representing each of the clusters, as shown in the header of each figure."
In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).
To give clear instructions on how to complete a recipe, use these sections as follows:
This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.
This section contains the steps required to follow the recipe.
This section usually consists of a detailed explanation of what happened in the previous section.
This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.
This section provides helpful links to other useful information for the recipe.
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packt.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packt.com.
This chapter begins with an introduction to Matplotlib, including the architecture of Matplotlib and the elements of a figure, followed by the recipes. The following are the recipes that will be covered in this chapter:
Working in interactive mode
Working in non-interactive mode
Reading from external files and plotting
How to change and reset default environment variables
Matplotlib is a cross-platform Python library for plotting two-dimensional graphs (also called plots). It can be used in a variety of user interfaces such as Python scripts, IPython shells, Jupyter Notebooks, web applications, and GUI toolkits. It can be used to develop professional reporting applications, interactive analytical applications, complex dashboard applications or embed into web/GUI applications. It supports saving figures into various hard-copy formats as well. It also has limited support for three-dimensional figures. It also supports many third-party toolkits to extend its functionality.
Matplotlib has a three-layer architecture: backend, artist, and scripting, organized logically as a stack. Scripting is an API that developers use to create the graphs. Artist does the actual job of creating the graph internally. Backend is where the graph is displayed.
This is the bottom-most layer where the graphs are displayed on to an output device. This can be any of the user interfaces that Matplotlib supports. There are two types of backends: user interface backends (for use in pygtk, wxpython, tkinter, qt4, or macosx, and so on, also referred to as interactive backends) and hard-copy backends to make image files (.png, .svg, .pdf, and .ps, also referred to as non-interactive backends). We will learn how to configure these backends in later Chapter 9, Developing Interactive Plots and Chapter 10, Embedding Plots in a Graphical User Interface.
This is the middle layer of the stack. Matplotlib uses the artist object to draw various elements of the graph. So, every element (see elements of a figure) we see in the graph is an artist. This layer provides an object-oriented API for plotting graphs with maximum flexibility. This interface is meant for seasoned Python programmers, who can create complex dashboard applications.
This is the topmost layer of the stack. This layer provides a simple interface for creating graphs. This is meant for use by end users who don't have much programming expertise. This is called a pyplot API.
The high-level Matplotlib object that contains all the elements of the output graph is called a figure. Multiple graphs can be arranged in different ways to form a figure. Each of the figure's elements is customizable.
The following diagram is the anatomy of a figure, containing all its elements:
axes is a sub-section of the figure, where a graph is plotted. axes has a title, an x-label and a y-label. A figure can have many such axes, each representing one or more graphs. In the preceding figure, there is only one axes, two line graphs in blue and red colors.
These are number lines representing the scale of the graphs being plotted. Two-dimensional graphs have an x axis and a y axis, and three-dimensional graphs have an x axis, a y axis, and a z axis.
This is the name given to various elements of the figure, for example, x axis label, y axis label, graph label (blue signal/red signal in the preceding figure Anatomy of a figure), and so on.
When there are multiple graphs in the axes (as in the preceding figure Anatomy of a figure), each of them has its own label, and all these labels are represented as a legend. In the preceding figure, the legend is placed at the top-right corner of the figure.
It is the name given to each of the axes. The figure also can have its own title, when the figure has multiple axes with their own titles. The preceding figure has only one axes, so there is only one title for the axes as well as the figure.
Each axis (x, y, or z) will have a range of values that are divided into many equal bins. Bins are chosen at two levels. In the preceding figure Anatomy of a figure, the x axis scale ranges from 0 to 4, divided into four major bins (0-1, 1-2, 2-3, and 3-4) and each of the major bins is further divided into four minor bins (0-0.25, 0.25-0.5, and 0.5-0.75). Ticks on both sides of major bins are called major ticks and minor bins are called minor ticks, and the names given to them are major ticklabels and minor ticklabels.
Boundaries of the figure are called spines. There are four spines for each axes(top, bottom, left, and right).
For easier readability of the coordinates of various points on the graph, the area of the graph is divided into a grid. Usually, this grid is drawn along major ticks of the x and y axis. In the preceding figure, the grid is shown in dashed lines.
Matplotlib can be used in an interactive or non-interactive modes. In the interactive mode, the graph display gets updated after each statement. In the non-interactive mode, the graph does not get displayed until explicitly asked to do so.
You need working installations of Python, NumPy, and Matplotlib packages.
Using the following commands, interactive mode can be set on or off, and also checked for current mode at any point in time:
matplotlib.pyplot.ion()
to set the interactive mode
ON
matplotlib.pyplot.ioff()
to switch
OFF
the interactive mode
matplotlib.is_interactive()
to check whether the interactive mode is
ON
(
True
) or
OFF
(
False
)
Let's see how simple it is to work in interactive mode:
Set the screen output as the backend:
%matplotlib inline
Import the
matplotlib
and
pyplot
libraries. It is common practice in Python to import libraries with crisp synonyms. Note
plt
is the synonym for the
matplotlib.pyplot
pac
kage:
import matplotlib as mpl
import matplotlib.pyplot as plt
Set the interactive mode to ON:
plt.ion()
Check the status of interactive mode:
mpl.is_interactive()
You should get the output as
True
.
Plot a line graph:
plt.plot([1.5, 3.0])
You should see the following graph as the output:
Now add the axis labels and a title to the graph with the help of the following code:
# Add labels and titleplt.title("Interactive Plot") #Prints the title on top of graphplt.xlabel("X-axis") # Prints X axis label as "X-axis"plt.ylabel("Y-axis") # Prints Y axis label as "Y-axis"
After executing the preceding three statements, your graph should look as follows:
So, this is how the explanation goes:
plt.plot([1.5, 3.0])
plots a line graph connecting two points (0, 1.5) and (1.0, 3.0).
The
plot
command expects two arguments (
Python list, NumPy
array or
pandas
DataFrame) for the
x
and
y
axis respectively.
If only one argument is passed, it takes it as
y
axis co-ordinates and for
x
axis co-ordinates it takes the length of the argument provided.
In this example, we are passing only one list of two points, which will be taken as
y
axis coordinates.
For the
x
axis, it takes the default values in the range of 0 to 1, since the length of the list
[1.5, 3.0]
is 2.
If we had three coordinates in the list for
y
, then for
x,
it would take the range of 0 to 2.
You should see the graph like the one shown in
step 6.
plt.title("Interactive Plot")
, prints the title on top of the graph as
Interactive Plot.
plt.xlabel("X-axis")
, prints the
x
axis label as
X-axis.
plt.ylabel("Y-axis")
, prints the
y
axis label as
Y-axis.
After executing preceding three statements, you should see the graph as shown in
step 7.
If you are using Python shell, after executing each of the code statements, you should see the graph getting updated with title first, then the x axis label, and finally the y axis label.
If you are using Jupyter Notebook, you can see the output only after all the statements in a given cell are executed, so you have to put each of these three statements in separate cells and execute one after the other, to see the graph getting updated after each code statement.
You can add one more line graph to the same plot and go on until you complete your interactive session:
Plot a line graph:
plt.plot([1.5, 3.0])
Add labels and title:
plt.title("Interactive Plot")plt.xlabel("X-axis")plt.ylabel("Y-axis")
Add one more line graph:
plt.plot([3.5, 2.5])
The following graph is the output obtained after executing the code:
Hence, we have now worked in interactive mode.
In the interactive mode, we have seen the graph getting built step by step with each instruction. In non-interactive mode, you give all instructions to build the graph and then display the graph with a command explicitly.
Working on non-interactive mode won't be difficult either:
Start the kernel afresh, and import the
matplotlib
and
pyplot
libraries:
import matplotlibimport matplotlib.pyplot as plt
Set the interactive mode to OFF:
plt.ioff()
Check the status of interactive mode:
matplotlib.is_interactive()
You should get the output
False
.
Execute the following code; you will not see the plot on your screen:
# Plot a line graphplt.plot([1.5, 3.0])# Plot the title, X and Y axis labelsplt.title("Non Interactive Mode")plt.xlabel("X-axis")plt.ylabel("Y-axis")
Execute the following statement, and then you will see the plot on your screen:
# Display the graph on the screen plt.show()
Each of the preceding code statements is self-explanatory. The important thing to note is in non-interactive mode, you write complete code for the graph you want to display, and call plt.show() explicitly to display the graph on the screen.
The following is the output obtained:
By default, Matplotlib accepts input data as a Python list, NumPy array, or pandas DataFrame. So all external data needs to be read and converted to one of these formats before feeding it to Matplotlib for plotting the graph. From a performance perspective, NumPy format is more efficient, but for default labels, pandas format is convenient.
If the data is a .txt file, you can use NumPy function to read the data and put it in NumPy arrays. If the data is in .csv or .xlsx formats, you can use pandas to read the data. Here we will demonstrate how to read .txt, .csv, and .xlsx formats and then plot the graph.
Import the matplotlib.pyplot, numpy , and pandas packages that are required to read the input files:
Import the
pyplot
library with the
plt
synonym:
import matplotlib.pyplot as plt
Import the
numpy
library with the
np
synonym. The
numpy
library can manage n-dimensional arrays, supporting all mathematical operations on these arrays:
import numpy as np
Import the
pandas
package with
pd
as a synonym:
import pandas as pd
We will follow the order of .txt, .csv, and .xlsx files, in three separate sections.
After reading the data from any of the three formats (.txt, .csv, .xlsx) and format it to x and y variables, then we plot the graph using these variables as follows:
plt.plot(x, y)
Display the graph on the screen:
plt.show()
The following is the output obtained:
Depending on the format and the structure of the data, we will have to use the Python, NumPy, or pandas functions to read the data and reformat it into an appropriate structure that can be fed into the matplotlib.pyplot function. After that, follow the usual plotting instructions to plot the graph that you want.
Matplotlib uses the matplotlibrc file to store default values for various environment and figure parameters used across matplotlib functionality. Hence, this file is very long. These default values are customizable to apply for all the plots within a session.
You can use the print(matplotlib.rcParams) command to get all the default parameter settings from this file.
The matplotlib.rcParams command is used to change these default values to any other supported values, one parameter at a time. The matplotlib.rc command is used to set default values for multiple parameters within a specific group, for example, lines, font, text, and so on. Finally, the matplotlib.rcdefaults() command is used to restore default parameters.
The following code block provides the path to the file containing all configuration the parameters:
# Get the location of matplotlibrc fileimport matplotlibmatplotlib.matplotlib_fname()
You should see the directory path like the one that follows. The exact directory path depends on your installation:
'C:\\Anaconda3\\envs\\keras35\\lib\\site-packages\\matplotlib\\mpl- data\\matplotlibrc'
The matplotlib.rc and matplotlib.rcParams commands overwrite the default values for specified parameters as arguments in these commands. These new values will be used by the pyplot tool while plotting the graph.
You can reset all the parameters to their default values, using the rsdefaults() command, as shown in the following block:
# To restore all default parametersmatplotlib.rcdefaults() plt.plot(x,y)plt.show()
The graph will look as follows:
.
In this chapter, we will cover recipes for plotting the following graphs:
Line plot
Bar plot
Scatter plot
Bubble plot
Stacked plot
Pie plot
Table chart
Polar plot
Histogram
Box plot
Violin plot
Heatmap
Hinton diagram
Images
Contour plot
Triangulations
Stream plot
Path
A picture is worth a thousand words, and the visualization of data plays a critical role in finding hidden patterns in the data. Over a period of time, a variety of graphs have been developed to represent different relationships between different types of variables. In this chapter, we will see how to use these different graphs in different contexts and how to plot them using Matplotlib.
The line plot is used to represent a relationship between two continuous variables. It is typically used to represent the trend of a variable over time, such as GDP growth rate, inflation, interest rates, and stock prices over quarters and years. All the graphs we have seen in Chapter 1, Anatomy of Matplotlib are examples of a line plot.
We will use the Google Stock Price data for plotting time series line plot. We have the data (date and daily closing price, separated by commas) in a .csv file without a header, so we will use the pandas library to read it and pass it on to the matplotlib.pyplot function to plot the graph.
Let's now import required libraries with the following code:
import matplotlib.pyplot as pltimport pandas as pd
The following is the explanation of the code:
pd.read_csv()
function specifies the following:
header=None
, input file has no header
delimiter=','
,
date
and
price
are separated by a comma (,)
Read the data into the
stock
DataFrame
The
stock.columns
command assigns names for each of the attributes,
date
and
price
, within the
stock
DataFrame.
The
pd.to_datetime()
function converts the date from the character format to the date time format. The format:
%d-%m-%Y
argument specifies the format of the date in the input file.
stock.set_index()
sets the
date
column as the index, so that the
price
column can represent the time series data, which is understood by the
plot
command.
The following graph is the output you should get from the preceding code block:
Bar plots are the graphs that use bars to compare different categories of data. Bars can be shown vertically or horizontally, based on which axis is used for a categorical variable. Let's assume that we have data on the number of ice creams sold every month in an ice cream parlor over a period of one year. We can visualize this using a bar plot.
We will use the Python calendar package to map numeric months (1 to 12) to the corresponding descriptive months (January to December).
Before we plot the graph, we need to import the necessary packages:
import matplotlib.pyplot as pltimport numpy as npimport calendar
Here is the explanation of the preceding code blocks:
Older versions of Matplotlib accept only floating-point data types as their arguments for data. So, the months have to be represented in a numerical format.
month_num
and
units_sold
are Python lists representing the number of units sold in each month of a year.
plt.subplots()
allows us to define the layout of the figure in terms of the number of graphs and how they should be organized within the figure. We will learn more about it in
Chapter 3
,
Plotting Multiple Charts, Subplots, and Figures
and
Chapter 6
,
Plotting with Advanced Features
. In this case, we are using it to get access to the axes on which we are plotting the bar graph so that we can annotate them with the actual data representing the bar.
If you recall from
Chapter 1
,
Anatomy of Matplotlib,
we have seen that axes is the individual plot within the figure
.
Changing the month format from the numerical format to its corresponding month name on the
x
axis.
calendar.month_name[1:13]
will return
January to December
, whereas
plt.xticks()
changes
x
axis tickers from numeric 1 to 12, to January to December, for better readability.
ax.text()
within the
for
loop annotates each bar with its corresponding data value.
Arguments for this function specify where exactly the data text has to be placed over the bar: first, get the current bar's
x
and
y
coordinates, and then
add
bar_width/2
to the
x
co-ordinate with
1.002*
height
being the
y
co-ordinate; then, using the
va
and
ha
arguments, we align the text centrally over the bar:
A scatter plot is used to compare distribution of two variables and see whether there is any correlation between them. If there are distinct clusters/segments within the data, it will be clear in the scatter plot.
Import the following libraries:
import matplotlib.pyplot as pltimport pandas as pd
We will use pandas to read the Excel files.
The explanation for the code follows:
plt.figure(figsize=(10,6))
overwrites the default figure size with size
(10, 6)
.
pd.read_excel()
reads the data and assigns values to the
x
and
y
axes coordinates.
plt.scatter(x,y)
plots the scatter plot.
plt.xlabel()
and
plt.ylabel()
set
x
and
y
axes labels for better readability.
Display the graph on the terminal. You should see the following chart:
It is clearly visible that there is no relationship between the age and the weight of people, as the points are scattered. If a correlation is present between the two elements depicted, we would observe a pattern of a straight line or a curve.
