Discover millions of ebooks, audiobooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

Matplotlib 3.0 Cookbook: Over 150 recipes to create highly detailed interactive visualizations using Python
Matplotlib 3.0 Cookbook: Over 150 recipes to create highly detailed interactive visualizations using Python
Matplotlib 3.0 Cookbook: Over 150 recipes to create highly detailed interactive visualizations using Python
Ebook1,270 pages5 hours

Matplotlib 3.0 Cookbook: Over 150 recipes to create highly detailed interactive visualizations using Python

Rating: 0 out of 5 stars

()

Read preview

About this ebook

Build attractive, insightful, and powerful visualizations to gain quality insights from your data




Key Features



  • Master Matplotlib for data visualization


  • Customize basic plots to make and deploy figures in cloud environments


  • Explore recipes to design various data visualizations from simple bar charts to advanced 3D plots





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



  • Develop simple to advanced data visualizations in Matplotlib


  • Use the pyplot API to quickly develop and deploy different plots


  • Use object-oriented APIs for maximum flexibility with the customization of figures


  • Develop interactive plots with animation and widgets


  • Use maps for geographical plotting


  • Enrich your visualizations using embedded texts and mathematical expressions


  • Embed Matplotlib plots into other GUIs used for developing applications


  • Use toolkits such as axisartist, axes_grid1, and cartopy to extend the base functionality of Matplotlib



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.

LanguageEnglish
Release dateOct 23, 2018
ISBN9781789138665
Matplotlib 3.0 Cookbook: Over 150 recipes to create highly detailed interactive visualizations using Python

Related to Matplotlib 3.0 Cookbook

Related ebooks

Computers For You

View More

Related articles

Reviews for Matplotlib 3.0 Cookbook

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Matplotlib 3.0 Cookbook - Srinivasa Rao Poladi

    Matplotlib 2.x Cookbook

    Matplotlib 3.0 Cookbook

    Over 150 recipes to create highly detailed interactive visualizations using Python

    Srinivasa Rao Poladi

    BIRMINGHAM - MUMBAI

    Matplotlib 3.0 Cookbook

    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 Shetty

    Acquisition Editor: Namrata Patil

    Content Development Editor: Unnati Guha

    Technical Editor: Sayli Nikalje

    Copy Editor: Safis Editing

    Project Coordinator: Manthan Patel

    Proofreader: Safis Editing

    Indexer: Rekha Nair

    Graphics: Jisha Chirayil

    Production 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.io

    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.

    Why subscribe?

    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

    Packt.com

    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. 

    Contributors

    About the author

    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.

    About the reviewer

    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.

    Packt is searching for authors like you

    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.

    Table of Contents

    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

    Preface

    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.

    Who this book is for

    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.

    What this book covers

    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.

    To get the most out of this book

    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.

    Download the example code files

    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!

    Download the color images

    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.

    Conventions used

    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 plt

    import pandas as pd

    import numpy as np

    from 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."

    Warnings or important notes appear like this.

    Tips and tricks appear like this.

    Sections

    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:

    Getting ready

    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.

    How to do it…

    This section contains the steps required to follow the recipe.

    How it works…

    This section usually consists of a detailed explanation of what happened in the previous section.

    There's more…

    This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.

    See also

    This section provides helpful links to other useful information for the recipe.

    Get in touch

    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.

    Reviews

    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.

    Anatomy of Matplotlib

    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 

    Introduction

    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.

    Please note that all the examples in this book are tested with Matplotlib 3.0 and Jupyter Notebook 5.1.0.

    Architecture of Matplotlib

    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.

    Backend layer

    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.

    Artist layer

    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.

    Scripting layer

    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.

    Elements of a figure

    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.

    Figure

    The following diagram is the anatomy of a figure, containing all its elements:

    Anatomy of a figure (Source : http://diagramss.us/plotting-a-graph-in-matlab.html)

    Axes

    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.

    Axis

    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.

    Don't get confused between axes and axis. Axis is an element of axes. Grammatically, axes is also the plural for axis, so interpret the meaning of axes depending on the context, whether multiple axis elements are being referred to or an axes object is being referred to.

    Label

    This is the name given to various elements of the figure, for example, axis label, axis label, graph label (blue signal/red signal in the preceding figure Anatomy of a figure), and so on.

    Legend

    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.

    Title

    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.

    Ticklabels

    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 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.

    Spines

    Boundaries of the figure are called spines. There are four spines for each axes(top, bottom, left, and right).

    Grid

    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.

    Working in interactive mode

    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. 

    Getting ready

    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)

    How to do it...

    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 package:

    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 title

    plt.title(Interactive Plot) #Prints the title on top of graph

    plt.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:

    How

    Enjoying the preview?
    Page 1 of 1