Reader small image

You're reading from  Matplotlib 3.0 Cookbook

Product typeBook
Published inOct 2018
PublisherPackt
ISBN-139781789135718
Edition1st Edition
Right arrow
Authors (2):
Srinivasa Rao Poladi
Srinivasa Rao Poladi
author image
Srinivasa Rao Poladi

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.
Read more about Srinivasa Rao Poladi

Nikhil Borkar
Nikhil Borkar
author image
Nikhil Borkar

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, the worked with Morgan Stanley Capital International (MSCI) as a Global RFP Project Manager.
Read more about Nikhil Borkar

View More author details
Right arrow

Using the axes_grid1 Toolkit

In this chapter, we will learn about features of the axes_grid1 toolkit. We will cover the following recipes:

  • Plotting twin axes using the axisartst and axesgrid1 toolkits
  • Using AxesDivider to plot a scatter plot and associated histograms
  • Using AxesDivider to plot a colorbar
  • Using ImageGrid to plot images with a colorbar in a grid
  • Using inset_locator to zoom in on an image
  • Using inset_locator to plot inset axes

Introduction

The axes_grid1 toolkit has been designed to provide a collection of helper classes to plot images with colorbars in a grid, with the proper alignment of colorbars with the image size. It can be used to zoom in on a portion of the image, plot inset axes (similar to picture in picture), and plot a two-dimensional graph and an associated histogram of each of the two variables, parasite axes, and anchored artists.

Plotting twin axes using the axisartist and axesgrid1 toolkits

In Chapter 4, Developing Visualizations for Publishing Quality, we learned how to plot two different scales on the left and right axes of a plot using twin axes methods. In Chapter 9, Developing Interactive Plots, we learned how to plot different units of measurements on multiple y axes, again using the twinx method for interactive analysis. In this recipe, we will learn how to plot multiple scales on various y axes using the main Matplotlib twinx method, as well as using the axisartist and axes_grid1 toolkits.

Getting ready

We will first plot our graph using the Matplotlib twinx() method. Then, in the There's more... section of this recipe, we will plot...

Using AxesDivider to plot a scatter plot and associated histograms

In this recipe, we will learn how to use the AxesDivider class of axes_grdi1 to draw a bivariate plot on the main axes, and two univariate plots, on any two sides of the main axes. This helps in visualizing the relationship between two variables, and the distribution of the same two variables individually all in one figure (though three different axes/plots).

Technically, variables plotted on the main axes and univariate plots on the two sides of the main axes can be different. And you can choose any two sides out of the four sides of the main axes for univariate plots. However, the usual practice is to plot on the top and the right side of the main axes.

In this recipe, we will plot a scatter graph on the main axes with two variables, and at the top and right-hand side of the main axes, we will plot a histogram...

Using AxesDivider to plot a colorbar

In this recipe, we will learn how to use AxesDivider to plot the colorbar for an image, ensuring that it is properly aligned with the image. We have used pyplot.colorbar() many times, but at times, colorbar goes beyond the image boundaries. With AxesDivider and ImageGrid, we will not see this problem, as shall be shown in this recipe.

We will plot the image on three axes—one using a regular pyplot.colorbar() without AxesDivider; the second using AxesDivider and a vertical colorbar on the right-hand side; and the third again using AxesDivider, but this time with a horizontal colorbar on top of the image.

Getting ready

You'll need to import the required libraries using the following...

Using ImageGrid to plot images with a colorbar in a grid

In Chapters 3, Plotting Multiple Graphs, Subplots and Figures and Chapter 6, Plotting with Advanced Features, we had learned how to draw multiple plots on a grid with various grid options. When you try to plot images with a colorbar on a grid with those options, the colorbar alignment may not be the way we want it. The ImageGrid helper function of the axes_grid1 toolkit enables the plotting of images with a colorbar in a grid with proper alignment. We will learn how to use these functions in this recipe.

We will plot the same correlation map in three different plots in a row, but use different features of the grid to demonstrate most, if not all, of the available features.

Getting ready

...

Using inset_locator to zoom in on an image

In Chapter 6, Plotting with Advanced Features, we learned how to plot inset axes within the main axes, using the object-oriented interface of Matplotlib. In this recipe and the next one, we will learn how to use the inset_locator helper functions of the axes_grid1 toolkit to create inset axes and plot images and other graphs.

In this recipe, we will learn how to zoom in on a portion of an image and display it in the inset axes of the same main axes.

We will plot the image on two axes and zoom in on different portions of the image in each of the plots.

Getting ready

You'll need to import the required libraries using the following commands:

import matplotlib.pyplot as plt
import...

Using inset_locator to plot inset axes

In this recipe, we will learn to plot a scatter graph on the main axes and create a histogram of each of the two variables on the two inset axes. We will use the familiar Iris dataset to do this.

Getting ready

You'll need to import the required libraries using the following commands:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

How to do it...

Here is the step-by-step approach to plotting our graph:

  1. Load the Iris dataset and map the x and y coordinates...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Matplotlib 3.0 Cookbook
Published in: Oct 2018Publisher: PacktISBN-13: 9781789135718
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Srinivasa Rao Poladi

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.
Read more about Srinivasa Rao Poladi

author image
Nikhil Borkar

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, the worked with Morgan Stanley Capital International (MSCI) as a Global RFP Project Manager.
Read more about Nikhil Borkar