Matplotlib 3.0 Cookbook
eBook - ePub

Matplotlib 3.0 Cookbook

Over 150 recipes to create highly detailed interactive visualizations using Python

  1. 676 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Matplotlib 3.0 Cookbook

Over 150 recipes to create highly detailed interactive visualizations using Python

About this book

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.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

Exploratory Data Analysis Using the Seaborn Toolkit

In this chapter, we will learn how to use one more third-party toolkit, seaborn, with the recipes that plot the following types of graphs:
  • Relational plots (sns.relplot):
    • Line plots (sns.lineplot)
    • Scatter plots (sns.scatterplot)
  • Categorical plots (sns.catplot):
    • Strip and swarm plots (sns.stripplot, sns.swarmplot)
    • Box and boxn plots (sns.boxplot, sns.boxnplot)
    • Bar and count plots (sns.barplot, sns.countplot)
    • Violin plots (sns.violinplot)
    • Point plots (sns.pointplot)
  • Distribution plots:
    • Distribution, Kernel Density Estimate (KDE), and rug plots (sns.distplot, sns.kdeplot, sns.rugplot)
  • Regression plots:
    • Regression plots and residual plots (sns.regplot, sns.residplot)
    • Lm plots (sns.lmplot)
  • Multi-plot grids:
    • Joint plots and joint grid plots (sns.jointplot, sns.JointGrid)
    • Pair Plots and pair grid plots (sns.pairplot, sns.PairGrid)
    • Facet grids (sns.FacetGrid)
  • Matrix plots:
    • Heatmaps (sns.heatmap)
    • Cluster maps (sns.clustermap)

Introduction

Seaborn is a powerful visualization tool built on top of Matplotlib. It makes multi-variable exploratory data analysis easier and intuitive, and it adds a few new types of plots, and its background styles and color maps are much more pleasing. It has many built-in statistical functions, making it a preferred tool for statistical data analysis. It also has quite elaborate online documentation, which you can find at https://seaborn.pydata.org/index.html.
We will use two datasets to demonstrate most of the seaborn features. One dataset, Wine Quality, is already familiar to you, and we will introduce a new dataset containing snack sales data from a fictitious snack shop. Instead of reading these files many times in each of the recipes, we will describe both of them in this section, and subsequently we will just use them for plotting the graphs. This is a slight deviation from the approach we have taken so far in this book. Both these data files can be found in the code library for this chapter.

Snacks Sales dataset

This dataset (provided in the code library) contains information on the sales of various items for a given date, day of the week, whether it is a weekend, and whether there was promotion on that day. We have data for three years: 2015, 2016, and 2017. The first five rows of the dataset look as follows:
Then, we will add two derived variables that help plot the required graphs:Month (1 to 12) and Quarter (1 to 4). After adding these variables, the first five rows look as follows:
The following code block reads the Excel file and adds additional variables:
import pandas as pd
import numpy as np

snacks_sales = pd.read_csv('Snacks_Data.csv')
snacks_sales['Month'] = pd.DatetimeIndex(snacks_sales['Date']).month
Quarter_Mapping = {1:1, 2:1, 3:1, 4:2, 5:2, 6:2, 7:3, 8:3, 9:3, 10:4,
11:4, 12:4}
snacks_sales['Quarter'] = snacks_sales['Month'].map(Quarter_Mapping)

Wine Quality

This dataset has 11 attributes that influence the quality of the wine. The Quality rating varies from 3 to 8, and then we map 3 and 4 to Low, 5 and 6 to Med, and 7 and 8 to High to create a new variable: Quality.
The first five rows of the dataset looks as follows:
We will also compute the correlation matrix for the Wine Quality dataset, which we will use in some of the plots.
The following is the code block to read and add additional variables to the Wine Quality dataset:
import pandas as pd

# Read the data from a csv file into pandas data frame
wine_quality = pd.read_csv('winequality.csv', delimiter=';')

# Map numeric Quality codes to "Low", "Med" and "High" qualitative
ratings
quality_map = {3:'Low', 4: 'Low', 5:'Med', 6:'Med', 7:'High', 8:'High'}
wine_quality['Quality'] = wine_quality['quality'].map(quality_map)

# compute correlation matrix
corr = wine_quality.corr()

# Display the first 5 records of wine_quality dataset, and unique
values of quality variable
wine_quality.head()
set(wine_quality.quality)

Semantic and facet variables

Apart from two variables whose relationship is plotted in a two-dimensional graph, seaborn allows the plotting of the influence of three additional variables on the relationship between the two main variables. These three variables are called semantic variables. They are referred to as hue, size, and style, which act as arguments to the given plot function.
For each unique value of hue, there will be one relationship plot; similarly, for each unique value of style, there will be one relationship plot. If there are two unique values in the hue variable (for example, Yes and No) and there are two unique values in the style variable (for example, s and D markers), then there will be 2 * 2 = 4 relationship plots (Yes & s, Yes & D, No & s, No & D combinations). Various hue values are plotted in different colors, and various style values are plotted in different line or marker styles.
Similarly, the size variable influences the size of the points being plotted for the two main variables. Unlike the Matplotlib scatter function, here, the size variable range is divided into multiple bins, and points are assigned to these bins. In the Matplotlib scatter function, each point is mapped to a different value in the size variable individually.
In addition to t...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Packt Upsell
  4. Contributors
  5. Preface
  6. Anatomy of Matplotlib
  7. Getting Started with Basic Plots
  8. Plotting Multiple Charts, Subplots, and Figures
  9. Developing Visualizations for Publishing Quality
  10. Plotting with Object-Oriented API
  11. Plotting with Advanced Features
  12. Embedding Text and Expressions
  13. Saving the Figure in Different Formats
  14. Developing Interactive Plots
  15. Embedding Plots in a Graphical User Interface
  16. Plotting 3D Graphs Using the mplot3d Toolkit
  17. Using the axisartist Toolkit
  18. Using the axes_grid1 Toolkit
  19. Plotting Geographical Maps Using Cartopy Toolkit
  20. Exploratory Data Analysis Using the Seaborn Toolkit
  21. Other Books You May Enjoy

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Matplotlib 3.0 Cookbook by Srinivasa Rao Poladi in PDF and/or ePUB format, as well as other popular books in Informatica & Modellazione e design di dati. We have over one million books available in our catalogue for you to explore.