Search icon CANCEL
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Can$12.99 | ALL EBOOKS & VIDEOS
Save more on purchases! Buy 2 and save 10%, Buy 3 and save 15%, Buy 5 and save 20%
Mastering Matplotlib 2.x
Mastering Matplotlib 2.x

Mastering Matplotlib 2.x: Effective Data Visualization techniques with Python

By Benjamin Walter Keller
Free Trial
Book Nov 2018 214 pages 1st Edition
eBook
Can$33.99 Can$12.99
Print
Can$41.99 Can$28.99
Subscription
Free Trial
eBook
Can$33.99 Can$12.99
Print
Can$41.99 Can$28.99
Subscription
Free Trial

What do you get with a Packt Subscription?

Free for first 7 days. $15.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Mastering Matplotlib 2.x

Heavy Customization

This book will teach us about advanced Matplotlib plots. It will enable you to go from data to plot to insight, in order to take raw numbers, raw information, and turn them into a visualization, which will allow us to build within our mind the actual bit of insight on how the data behaves. This will also focus on the advanced tools to derive more subtle insights from your data.

In this chapter, we will be focusing on the advanced tools of plotting so that you can really derive more subtle insights from your data. The prerequisites for this course give us a basic understanding of Python and the ability to use NumPy to work with array data.

We will learn about the following topics:

  • Using style sheets to customize our plot's appearance
  • Working with Matplotlib colors
  • Building multi-panel plots with complex layouts
  • How to configure Matplotlib to use our preferences whenever we start up a new Matplotlib session

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.

Deep diving into color

When it comes to choosing color maps, we have a near infinite set of choices available to us, so it makes a lot of sense to think about the theory of why certain color maps are designed in the way they are.

A classic paper on the theory of Choosing a Colour Sequence for Univariate Maps was published by Colin Ware over 30 years ago. In this paper, Dr. Ware defines two different important things to think about when choosing a color map:

  • How a color map conveys the metric value of individual points within the image
  • How that color map conveys form—the shape of the different points interacting within each other

Questions to ask when choosing a color map

Different color maps can perform better or worse at conveying metrics, letting the viewer know the value of pixels on the screen, and conveying form—showing how those pixels relate to one another. Whether or not form or metrics or both are important for a given image is really key to deciding what kind of color map to use for that image.

The different questions that can be asked when choosing a color map are as follows:

  • Do we need to think whether or not the shape, the value, or-again-both are important for that field?
  • Are there critical values or transitions in the data?
  • Are there ranges of numbers that really want to pop out to the viewer?
  • What is the intuitive choice of colors for the dataset?

For most people, a dataset that conveys temperature should usually have hot red colors to denote high temperatures and cool blue colors to denote cold temperatures; to reverse that would violate the intuitive sense that the viewer has and set up an initial set of stressors in their mind that's going to make interpreting the image harder than it needs to be.

Taking an example, have a look at the color map here, showing population density in the United States:

Using one kind of color map—one that's not perceptually uniform—washes out a lot of the high values. Simply changing it to another different kind of color map that has less high-valued whitish colors at the high end allows you to see more detail in the higher density, more populated eastern parts of the US.

To take a look at all of the color maps that Matplotlib provides, visit the website provided here: http://matplotlib.org/examples/color/colormaps_reference.html. This offers a little swatch of each different color map to give you an idea of what the color map can do in terms of the ranges.

Before we move ahead with the code, we will start by importing the following set of default packages, as shown here:

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

Using color maps

Now, we can choose a color map from Matplotlib's selection of color maps by passing the cmap='inferno' color map:

from scipy.ndimage.filters import gaussian_filter
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10))

The following displays the image using the inferno color map:

We can see the differences in the value between dark cool colors and white hot colors. The perceptually uniform color maps are good at conveying both metric and form. There are color maps that you might want to choose for different kinds of purposes.

For example, the seismic color map, which is one of the diverging color maps, is very good for showing differences between the middle values of your plot. So, divergences towards the highly positive or highly negative in most of the values near the median of the pixels are hard to distinguish. What instead pops out to the viewer are the highly positive and highly negative ranges, as shown in the following code and image:

from scipy.ndimage.filters import gaussian_filter
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10), cmap='seismic')

The following is the output of the preceding code:

Another option is one of the miscellaneous color maps, flag. This is a really good example of a color map that maximizes the amount of information available in terms of the form that is the shape of the data, but completely loses any metric information. This color map actually cycles, so there are multiple different values corresponding to red, white, black, and blue. In a way, it gives you a set of contours much like the contour command does:

# Choose a new colormap
from scipy.ndimage.filters import gaussian_filter
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10), cmap='flag')

The following is the output of the preceding code:

We can create our own color maps using Matplotlib's colors module. We can create a color map using the listed method from colors that will create a color map from a list of named colors available for our image. So, it will actually create something that looks a lot like a filled contour plot:

# Generate a custom discrete colormap w/ ListedColormap
from matplotlib import colors
from scipy.ndimage.filters import gaussian_filter
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10), cmap=colors.ListedColormap(['r', 'b', 'g']))
plt.colorbar()

Hence, from the preceding code, we get the output contour map showing that the colors are split into three separate pieces. The following plot shows a discrete color map, again, something that's very good for showing form but not particularly good for showing metrics unless we had to include a large range of colors that would split this up and make it more continuous:

Now, this linear segmented color map requires a call structure, which is a basic ListedColor.

We will include the first argument as my_map as we are creating a new one, which also requires a dictionary as its second argument, called cdict:

# Generate a custom discrete colormap w/ LinearSegmentedColormap
cdict = dict(red=[(0, 0, 0.5), (0.5, 1, 0.5), (1, 1, 0.5)],
green=[(0,0,1), (1, 0, 1)],
blue=[(0, 0, 1), (1, 0, 1)])
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10), cmap=colors.LinearSegmentedColormap('my_map',cdict))
plt.colorbar()

In the preceding code, we can see that there are three keyword arguments: red, green, and blue. Each of these is itself a list and these lists have elements that are three-element tuples; these tuples can be seen for the initial and last elements for the colors red, green, and blue.

Each of the three numbers in this tuple corresponds to the start value of that segment, the amount of that color we get in that value of the segment, and the alpha or transparency of that value in the segment. In the preceding plot, the color red becomes fully saturated at the end of our color map. The color red contains an extra element in the middle when we reach full saturation, as shown in the following colored map:

Adding a bit of opacity that allows between 0 and 0.5, as shown in the preceding code snippet, we get the following output:

The previous output shows a linear segmented color map which is a way to create any kind of complex sophisticated color map.

Let's look at another example, where colors are important in Matplotlib. We have seen already that the Matplotlib backend will automatically cycle through colors, so you can see in the next output that there are different waveforms:

To reset and interchange the colors, the axis object we learned has a set_prop_cycle argument. That cycler decides what the next color should be, so if we set the prop cycle to None after every second set of plot commands, we can see that the cycle changes the color and loops again, as follows:

# Color cycler: reset with Axes set_prop_cycle(None) method
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums), label='1')
plt.plot(1+nums, np.sin(nums), label='2')
plt.gca().set_prop_cycle(None)
plt.plot(2+nums, np.sin(nums), label='3')
plt.plot(3+nums, np.sin(nums), label='4')
plt.gca().set_prop_cycle(None)
plt.plot(4+nums, np.sin(nums), label='5')
plt.legend(loc='best')

We will get the following output:

We reset and go back to the beginning, but not only can we set this to None to reset our cycler, we can use the cycler module that Matplotlib provides. It's actually its own module, so we will import the cycler from itself and not from Matplotlib. By setting the prop cycle to the new color cycler, we can actually define our own cycler.

We have created a cycler that goes from red to blue. We will include another one here, which is cyan, hence we should go from red to cyan to blue and then loop over those again, as seen in the following code:

# Color cycler: custom with cycler
from cycler import cycler
color_cyc = cycler('color', ['red', 'cyan', 'blue'])
plt.gca().set_prop_cycle(color_cyc)
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums), label='1')
plt.plot(1+nums, np.sin(nums), label='2')
plt.plot(2+nums, np.sin(nums), label='3')
plt.plot(3+nums, np.sin(nums), label='4')
plt.plot(4+nums, np.sin(nums), label='5')
plt.legend(loc='best')

Hence, here, we get the following output:

This is a way of setting up your own set of color cycles, so, for example, if there are lines that have relationships to each other—so say you want to go throughout the red-orange-yellow-green-blue-indigo-violet spectrum with regards to your lines rather than the built-in cycle that Matplotlib provides—you can use this cycler to do it.

Working on non-trivial layouts

We will begin by looking at a subplot with one set of rows and three columns, as shown here:

# Standard Subplot
plt.subplot(131)
plt.subplot(132)
plt.subplot(133)

The following is the output of the preceding input:

Now, by default, it will provide three plots side by side of equal size, but if we want them on top of each other instead, we will choose three rows and one column:

# Standard Subplot
plt.subplot(311)
plt.subplot(312)
plt.subplot(313)

We will get the following output:

But what if these plots aren't necessarily supposed to show three identical ranges? In that case, we have the subplot2grid method:

# subplot2grid 
plt.subplot2grid((2,2), (0,0))
plt.subplot2grid((2,2), (1,0))
plt.subplot2grid((2,2), (0,1))

We will begin by making the same vertical plot as shown previously. Now, specify one row and three columns and in the second tuple, select the rows, because there's only one and three columns. Next, we will see that there's a grid with two rows and three columns. We will place everything in the first column within the first row and simply specify a column:

# subplot2grid 
plt.subplot2grid((2,3), (0,0))
plt.subplot2grid((2,3), (0,1))
plt.subplot2grid((2,3), (0,2))

The output will be as follows:

Now, to get this in a 2 x 3 grid, we need to have two plots underneath a third one; well in that case, what we want is a 2 x 2 grid, so one plot is going to be twice as wide and then two plots beneath, as shown here:

# Adjusting GridSpec attributes: w/h space
gs = mpl.gridspec.GridSpec(2,2)
plt.subplot(gs[0,0])
plt.subplot(gs[1,0])
plt.subplot(gs[1,1])

We will get the following output:

We have the positions right, but this has not actually made the plots bigger. To do that, we will have to pass an argument to the first one, plt.subplot2grid, called colspan=2, and using colspan, we say that this plot should span as shown in the following code:

# subplot2grid 
plt.subplot2grid((2,2), (0,0), colspan=2)
plt.subplot2grid((2,2), (1,0))
plt.subplot2grid((2,2), (0,1))

We will get the following output::

There is also another option as well for doing similar things, but in the vertical direction with row span. Therefore, we can perform the following action:

# subplot2grid 
plt.subplot2grid((2,2), (0,0))
plt.subplot2grid((2,2), (1,0))
plt.subplot2grid((2,2), (0,1), rowspan=2)

From the preceding code, we will get the following output:

We can do the same thing using a GridSpec object and in fact we can customize this a little more explicitly. After creating a GridSpec object, iterate it through the indices of that GridSpec to say where it will get placed. These indices are just like any two-dimensional NumPy array. So, for example, we can perform the following:

# Explicit GridSpec
gs = mpl.gridspec.GridSpec(2,2)
plt.subplot(gs[0,0])
plt.subplot(gs[0,1])

The following is the output of the preceding code:

By including a diagonal pair of plots, we get the following:

# Explicit GridSpec
gs = mpl.gridspec.GridSpec(2,2)
plt.subplot(gs[0,0])
plt.subplot(gs[1,1])

We will get the following output:

We can also make those plots span things using the range syntax that NumPy provides for us:

# Explicit GridSpec
gs = mpl.gridspec.GridSpec(2,2)
plt.subplot(gs[0,:])
plt.subplot(gs[1,1])

The preceding code gives the following output:

If, for example, we made this have three columns, we would get the following:

# Explicit GridSpec
gs = mpl.gridspec.GridSpec(2,3)
plt.subplot(gs[0,:])
plt.subplot(gs[1,1])

We will get the following output:

This shows that we can really dive into providing whatever kind of grid-like layout we want with plots of different sizes and shapes. You can also customize the amount of space between these plots. For example, look at the following grid:

We can adjust the amount of space between the grids using wspace for space between the plots:

# Adjusting GridSpec attributes: w/h space
gs = mpl.gridspec.GridSpec(2,2, wspace=0.5)
plt.subplot(gs[0,0])
plt.subplot(gs[0,1])
plt.subplot(gs[1,0])
plt.subplot(gs[1,1])

The preceding code gives the following output:

We can also adjust the amount of space between the grids using hspace for space between the plots:

# Adjusting GridSpec attributes: w/h space
gs = mpl.gridspec.GridSpec(2,2, hspace=0.5)
plt.subplot(gs[0,0])
plt.subplot(gs[0,1])
plt.subplot(gs[1,0])
plt.subplot(gs[1,1])

The ratios of the sizes can also be changed between the plots. So, say, for example, that we want our top row to be very tall and thin; we can specify a width and height ratio. If we pass width_ratios, we can decide how big each of these columns will appear relative to one another in terms of their width:

# Adjusting GridSpec attributes: width/height ratio
gs = mpl.gridspec.GridSpec(2,2, width_ratio=(1,2))
plt.subplot(gs[0,0])
plt.subplot(gs[0,1])
plt.subplot(gs[1,0])
plt.subplot(gs[1,1])

It sets the width of the second column to be twice the width of the first column:

When changing this to (2, 2), they will be equal, so it will literally just take a ratio of these numbers; you can pass whatever pair of ratios you want:

# Adjusting GridSpec attributes: width/height ratio
gs = mpl.gridspec.GridSpec(2,2, width_ratio=(2,2))
plt.subplot(gs[0,0])
plt.subplot(gs[0,1])
plt.subplot(gs[1,0])
plt.subplot(gs[1,1])

We will get the following output:

By changing the heights, we get the second set of plots—twice the height of the first one, as shown here:

# Adjusting GridSpec attributes: width/height ratio
gs = mpl.gridspec.GridSpec(2,2, height_ratio=(1,2))
plt.subplot(gs[0,0])
plt.subplot(gs[0,1])
plt.subplot(gs[1,0])
plt.subplot(gs[1,1])

We will see the following output:

These cannot only be integers—they can be floating-point numbers as well, as shown here:

# Adjusting GridSpec attributes: width/height ratio
gs = mpl.gridspec.GridSpec(2,2, height_ratios=(1.5,2))
plt.subplot(gs[0,0])
plt.subplot(gs[0,1])
plt.subplot(gs[1,0])
plt.subplot(gs[1,1])

By executing the preceding code, we will get the following output:

Hence, this is a quick and convenient way of setting up multi-panel plots that put the focus on the kinds of plots that we want to dominate. So, if you have a situation where, for example, you have a complex dataset where one piece of that visualization is the most important one but you have many supplemental plots that you want to show alongside it, using the subplot two grid and the grid spec object can be a nice way of building up those layouts.

The Matplotlib configuration files

In this section, we will look at the matplotlib configuration file, matplotlibrc, which contains all of the default configuration settings for how Matplotlib works on your system.

Matplotlibrc – where does it live?

matplotlibrc is multiple different configuration files; it's a hierarchy of configuration files that allows you to configure Matplotlib to behave differently for different users or different projects depending on what you need. By default, the hierarchy of which matplotlibrc config file overrides others begins with the one in the current directory, which oftentimes does not exist but ultimately will be the one that overrides all others.

The next one is within the matplotlibrc environment variable in that directory.

The third is within the config home, if you're running a Linux or BSD-based operating system with a directory called Matplotlib and a file called matplotlibrc.

Finally, with the Matplotlib installation pending on what system you're on, that installation living in some install directory will have a directory called Matplotlib data. A Matplotlib RC file that comes with Matplotlib installation is actually a fairly rich template that includes a wide variety of comments and example code which we will be discussing.

We will take a copy of the example file and put it in the .config/matplotlib file.

After opening the configuration file, we can see that it is quite big—in fact, it's 510 lines by default with the current version of Matplotlib, as shown here:

It also has a number of different options for configuring things such as fonts and appearance, as shown here:

There are also options such as the Latex behavior:

There are also options for the behavior of different backends, and we can configure the options that do not necessarily get covered by style sheets that would just be things like appearance, as follows:

The following screenshot shows the SVG writer:

Also, taking an example on what the default backend is, by default, we will set the backend to be TkAgg. We can, of course, change this depending on the system:

For example, we can change the color of text, as shown here:

# text.color : black

After uncommenting text.color (since this ultimately is the highest matplotlibrc in that hierarchy, overwriting versions of the .config file), there isn't a custom version in the directory with the Jupyter Notebooks. We will restart the kernel like we did earlier in this chapter:

Reimporting NumPy and Matplotlib, we can see here that after adding some text, hello, and inserting 0.5, we can see the colored text automatically without having to specify any other configuration options that you apply would get automatically applied without having to go in and change things by hand:

It's recommended to see all of the different options that Matplotlib provides. Go and take a look at that default template file:

Also, take a look at this documentation here: http://matplotlib.org/users/customizing.html#the-matplotlibrc-file. There is a little bit of extra text about the Matplotlib RC file. The template itself contains enough documentation. Understand this config file and look at all of the various options that are available to you.

So, if you want to configure your Matplotlib to behave in a more sensible way for your projects, it is recommended that you make a copy of this and start hacking away at it to tweak Matplotlib to do exactly what it is that you want it to.

Summary

In this chapter, we have learned about how to use style sheets to customize plot appearances, how to work with the color system within Matplotlib, how to tweak multi-panel plots to give more complex and appealing layouts for different kinds of plotting applications, and finally how to configure our Matplotlib installation.

In the next chapter, we will learn how to draw on plots to add annotations and highlights to really make the important features of a visualization pop out to the viewer.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Practical guide with hands-on examples to design interactive plots
  • Advanced techniques to constructing complex plots
  • Explore 3D plotting and visualization using Jupyter Notebook

Description

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

What you will learn

Deal with non-trivial and unusual plots Understanding Basemap methods Customize and represent data in 3D Construct Non-Cartesian and vector plots Design interactive plots using Jupyter Notebook Make movies for enhanced data representation

Product Details

Country selected

Publication date : Nov 29, 2018
Length 214 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781789617696
Category :

What do you get with a Packt Subscription?

Free for first 7 days. $15.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details


Publication date : Nov 29, 2018
Length 214 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781789617696
Category :

Table of Contents

7 Chapters
Preface Chevron down icon Chevron up icon
1. Heavy Customization Chevron down icon Chevron up icon
2. Drawing on Plots Chevron down icon Chevron up icon
3. Special Purpose Plots Chevron down icon Chevron up icon
4. 3D and Geospatial Plots Chevron down icon Chevron up icon
5. Interactive Plotting Chevron down icon Chevron up icon
6. Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Top Reviews
No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.