Book Image

Mastering Matplotlib 2.x

By : Benjamin Walter Keller
Book Image

Mastering Matplotlib 2.x

By: Benjamin Walter Keller

Overview of this book

In this book, you’ll get hands-on with customizing your data plots with the help of Matplotlib. You’ll start with customizing plots, making a handful of special-purpose plots, and building 3D plots. You’ll explore non-trivial layouts, Pylab customization, and more about tile configuration. You’ll be able to add text, put lines in plots, and also handle polygons, shapes, and annotations. Non-Cartesian and vector plots are exciting to construct, and you’ll explore them further in this book. You’ll delve into niche plots and visualize ordinal and tabular data. In this book, you’ll be exploring 3D plotting, one of the best features when it comes to 3D data visualization, along with Jupyter Notebook, widgets, and creating movies for enhanced data representation. Geospatial plotting will also be explored. Finally, you’ll learn how to create interactive plots with the help of Jupyter. Learn expert techniques for effective data visualization using Matplotlib 3 and Python with our latest offering -- Matplotlib 3.0 Cookbook
Table of Contents (7 chapters)

Customizing PyLab using style

We will start by importing numpy, matplotlib, and pyplot, as follows:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

We will also import matplotlib and also import a couple of extra lines to make our plots show up in a proper format:

%matplotlib inline
# Set up figure size and DPI for screen demo
plt.rcParams['figure.figsize'] = (6,4)
plt.rcParams['figure.dpi'] = 150

from scipy.ndimage.filters import gaussian_filter
plt.subplot(221)
plt.text(0.5, 0.5, 'hello')
plt.plot(np.arange(0,1,0.01), np.power(np.arange(0,1,0.01), 3))
plt.ylabel('Axis Label')
plt.subplot(222)
plt.scatter(np.random.normal(size=10), np.random.normal(size=10), c=np.random.normal(size=10))
plt.subplot(223)
plt.hist(np.random.normal(size=1000));
plt.hist(np.random.normal(1, size=1000));
plt.hist(np.random.normal(2, size=500));
plt.ylabel('Axis Label')
plt.xlabel('Axis Label')
plt.subplot(224)
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10))
plt.xlabel('Axis Label')

We will begin with the preceding big block of code and will make an array—a little grid of four plots showing four basic plot types which includes a line plot (top left), a scatter plot (top right), a histogram (bottom left), and an image plot (bottom right), along with the respective axis labels:

By default, Matplotlib will choose some fairly sensible choices for things like fonts, colors, and the other appearance attributes of these plots. These defaults aren't the only choices for appearance attributes that Matplotlib provides.

How to use styles to change the appearance of our plots

By using the style module within pyplot, you can see that when we call the function available, we actually get a list containing a number of different styles. Let's assume that each of these different styles acts to change the attributes and the appearance of the plots:

So, by using the plot.style.use method, we can load up any one of these default style sheets. Using the ggplot (as shown in the preceding output) will actually mimic the appearance of the ggplot library, as you can see in the following code:

# Using styles
plt.style.use('dark_background')
from scipy.ndimage.filters import gaussian_filter
plt.subplot(221)
plt.plot(np.arange(0,1,0.01), np.power(np.arange(0,1,0.01), 3))
plt.ylabel('Axis Label')
plt.subplot(222)
plt.scatter(np.random.normal(size=10), np.random.normal(size=10), c=np.random.normal(size=10))
plt.subplot(223)
plt.hist(np.random.normal(size=1000));
plt.hist(np.random.normal(1, size=1000));
plt.hist(np.random.normal(2, size=500));
plt.ylabel('Axis Label')
plt.xlabel('Axis Label')
plt.subplot(224)
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10))
plt.xlabel('Axis Label')

After calling the plt.style.use('ggplot') method, we have the same kind of plots shown, but the axis objects and the appearance of these has been changed fairly significantly. There exists a white grid on a gray background, the fonts have changed as well as their colors, and the default color choices have changed as well.

Hence, we see that the histogram will have different colors, as shown in the following output:

We can also change this to any other choice as well. If you're familiar with the Seaborn library, a Python library for doing more complicated analysis statistically, you can choose options that will mimic the Seaborn library:

# Using styles
plt.style.use('seaborn-talk')
from scipy.ndimage.filters import gaussian_filter
plt.subplot(221)
plt.plot(np.arange(0,1,0.01), np.power(np.arange(0,1,0.01), 3))
plt.ylabel('Axis Label')
plt.subplot(222)
plt.scatter(np.random.normal(size=10), np.random.normal(size=10), c=np.random.normal(size=10))
plt.subplot(223)
plt.hist(np.random.normal(size=1000));
plt.hist(np.random.normal(1, size=1000));
plt.hist(np.random.normal(2, size=500));
plt.ylabel('Axis Label')
plt.xlabel('Axis Label')
plt.subplot(224)
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10))
plt.xlabel('Axis Label')

Different Matplotlib styles

In this section, we will be learning about various styles provided by Matplotlib such as temporary styles or creating your own custom styles. The following are a few examples of different styles:

# Temporary styles
plt.style.use('classic')
from scipy.ndimage.filters import gaussian_filter
plt.subplot(221)
plt.plot(np.arange(0,1,0.01), np.power(np.arange(0,1,0.01), 3))
plt.ylabel('Axis Label')
plt.subplot(222)
with plt.style.context('ggplot'):
plt.scatter(np.random.normal(size=10), np.random.normal(size=10), c=np.random.normal(size=10))
plt.subplot(223)
plt.hist(np.random.normal(size=1000));
plt.hist(np.random.normal(1, size=1000));
plt.hist(np.random.normal(2, size=500));
plt.ylabel('Axis Label')
plt.xlabel('Axis Label')
plt.subplot(224)
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10))
plt.xlabel('Axis Label')

Using the dark background will give you an image that shows up nicely on a dark background, hence if you're building slides, you might want to use the dark background style sheet:

# Custom styles
plt.style.use('bigpoints')
from scipy.ndimage.filters import gaussian_filter
plt.subplot(221)
plt.plot(np.arange(0,1,0.01), np.power(np.arange(0,1,0.01), 3), 'ko')
plt.ylabel('Axis Label')
plt.subplot(222)
plt.scatter(np.random.normal(size=10), np.random.normal(size=10), c=np.random.normal(size=10))
plt.subplot(223)
plt.hist(np.random.normal(size=1000));
plt.hist(np.random.normal(1, size=1000));
plt.hist(np.random.normal(2, size=500));
plt.ylabel('Axis Label')
plt.xlabel('Axis Label')
plt.subplot(224)
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10))
plt.xlabel('Axis Label')

You will see the output, which shows the black background:

You can also choose style sheets temporarily. So, previously, we have chose a style sheet that affects all four plots. If, for example, I take my scatter plot and put it in a with plt.style.context block and choose a style sheet, let's say ggplot, you can see that we have actually overridden it, as shown here:

# Temporary styles
plt.style.use('classic')
from scipy.ndimage.filters import gaussian_filter
plt.subplot(221)
plt.plot(np.arange(0,1,0.01), np.power(np.arange(0,1,0.01), 3))
plt.ylabel('Axis Label')
plt.subplot(222)
with plt.style.context('ggplot'):
plt.scatter(np.random.normal(size=10), np.random.normal(size=10), c=np.random.normal(size=10))
plt.subplot(223)
plt.hist(np.random.normal(size=1000));
plt.hist(np.random.normal(1, size=1000));
plt.hist(np.random.normal(2, size=500));
plt.ylabel('Axis Label')
plt.xlabel('Axis Label')
plt.subplot(224)
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10))
plt.xlabel('Axis Label')

From the preceding code, we can see there's a small difference, but the color map has changed:

So, this one scatter plot has temporarily used a different set of choices for the appearance compared to the previous ones, so the other panels here are using the classic style sheet, whereas this is using ggplot, which changes the attributes of these points.

Creating your own styles

A question arises: can we actually make our own style sheet? The answer to that, of course, is yes. The first thing you need to know is where to put these style sheets.

So, when we run mpl.get_configdir, you can see we get a directory where the Matplotlib configuration options are stored and here we can actually place new style sheets:

# Where do we put out style sheets?
mpl.get_configdir()

We will thus generate our simple plot again by using a classic style sheet. Let's build a new style sheet; we will build something simple that will change just a single attribute. To build a new style sheet, we will follow these steps:

  1. We will make a directory called stylelib—this will be where style files are actually stored, and these style sheets will live in this stylelib directory:
$ ls
$ mkdir stylelib
$ cd stylelib
  1. We will make one style sheet and name it bigpoints.mplstyle.
.mplstyle is the file name extension for Matplotlib style sheets.
  1. Insert the marker size and make it 20:
lines.markersize: 20
  1. We will restart the kernel by clicking on the Kernel tab and then on Restart since we have created this new style sheet that only gets loaded in when Matplotlib is actually started up, as shown in the following screenshot:
  1. By going back to the Jupyter Notebook, we will reimport the following packages:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
# Set up figure size and DPI for screen demo
plt.rcParams['figure.figsize'] = (6,4)
plt.rcParams['figure.dpi'] = 150
  1. Next, we call plt.style.available and we see the addition to all of the big points, as shown in the following output:
  1. By running plt.style.use('bigpoints'), we can see it's changed the size of the points, as shown here:
# Custom styles
plt.style.use('bigpoints')
from scipy.ndimage.filters import gaussian_filter
plt.subplot(221)
plt.plot(np.arange(0,1,0.01), np.power(np.arange(0,1,0.01), 3))
plt.ylabel('Axis Label')
plt.subplot(222)
with plt.style.context('ggplot'):
plt.scatter(np.random.normal(size=10), np.random.normal(size=10), c=np.random.normal(size=10))
plt.subplot(223)
plt.hist(np.random.normal(size=1000));
plt.hist(np.random.normal(1, size=1000));
plt.hist(np.random.normal(2, size=500));
plt.ylabel('Axis Label')
plt.xlabel('Axis Label')
plt.subplot(224)
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10))
plt.xlabel('Axis Label')

We will get the following output:

  1. So, by typing ko, we get black dots. In the following image after the code snippet, you can see that those are quite big:
# Custom styles
plt.style.use('bigpoints')
from scipy.ndimage.filters import gaussian_filter
plt.subplot(221)
plt.plot(np.arange(0,1,0.01), np.power(np.arange(0,1,0.01), 3), 'ko')
plt.ylabel('Axis Label')
plt.subplot(222)
plt.scatter(np.random.normal(size=10), np.random.normal(size=10), c=np.random.normal(size=10))
plt.subplot(223)
plt.hist(np.random.normal(size=1000));
plt.hist(np.random.normal(1, size=1000));
plt.hist(np.random.normal(2, size=500));
plt.ylabel('Axis Label')
plt.xlabel('Axis Label')
plt.subplot(224)
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10))
plt.xlabel('Axis Label')

From the preceding code, we get the following output:

  1. We will go back and edit this and insert 50 points (more than twice what was shown earlier):
lines.markersize: 50
  1. After reimporting matplotlib, run plt.style.use('bigpoints') using a new style sheet; we can see that the points are bigger than they were before.

Styles can also be composed using two sets of styles, so you can combine the attributes of two different style sheets to generate things that have different combinations, and the way to do that is actually to use a list for plt.style.use.

By typing ggplot and composing that with dark_background, all of the subsequent changes that the dark background provides overwrites the changes that ggplot provides:

# Composing styles
plt.style.use(['ggplot', 'dark background'])
from scipy.ndimage.filters import gaussian_filter
plt.subplot(221)
plt.plot(np.arange(0,1,0.01), np.power(np.arange(0,1,0.01), 3), 'ko')
plt.ylabel('Axis Label')
plt.subplot(222)
plt.scatter(np.random.normal(size=10), np.random.normal(size=10), c=np.random.normal(size=10))
plt.subplot(223)
plt.hist(np.random.normal(size=1000));
plt.hist(np.random.normal(1, size=1000));
plt.hist(np.random.normal(2, size=500));
plt.ylabel('Axis Label')
plt.xlabel('Axis Label')
plt.subplot(224)
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10))
plt.xlabel('Axis Label')

We will get the following output:

So, in other words, what we have here is a combination of ggplot modules and dark background changes, hence all of the things that ggplot changes, the dark background does not. All of the changes that both ggplot and the dark background change use the changes from the dark background, and all of the changes that ggplot does not make but the dark background does get applied.

It's kind of like an overwrite, wherein the dark background is applied after ggplot is applied. You can make custom style sheets and then compose them so that each style sheet can work together. So, for example, you can have a style sheet that will change line attributes that you can then compose with a style sheet that will change the axis or label attribute, and you can use that to modify the built-in default Matplotlib style sheets.

Taking an example, if you really loved the look of ggplot but you didn't quite like one little attribute of it, you could actually go in and change that, as follows:

# Composing styles
plt.style.use(['ggplot'])
from scipy.ndimage.filters import gaussian_filter
plt.subplot(221)
plt.plot(np.arange(0,1,0.01), np.power(np.arange(0,1,0.01), 3))
plt.ylabel('Axis Label')
plt.subplot(222)
plt.scatter(np.random.normal(size=10), np.random.normal(size=10), c=np.random.normal(size=10))
plt.subplot(223)
plt.hist(np.random.normal(size=1000));
plt.hist(np.random.normal(1, size=1000));
plt.hist(np.random.normal(2, size=500));
plt.ylabel('Axis Label')
plt.xlabel('Axis Label')
plt.subplot(224)
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10))
plt.xlabel('Axis Label')

If you want to turn off these grid labels, you can create a new style sheet, disable grid labels, compose that with the ggplot style sheet, and get a plot that looks exactly the same but without those white grid lines, as follows:

In the next section, we are going to take a deep dive into looking at colors within Matplotlib and how to configure and customize them.