Reader small image

You're reading from  Matplotlib 2.x By Example

Product typeBook
Published inAug 2017
PublisherPackt
ISBN-139781788295260
Edition1st Edition
Right arrow
Authors (3):
Allen Yu
Allen Yu
author image
Allen Yu

Allen Yu, PhD, is a Chevening Scholar, 2017-18, and an MSC student in computer science at the University of Oxford. He holds a PhD degree in Biochemistry from the Chinese University of Hong Kong, and he has used Python and Matplotlib extensively during his 10 years of bioinformatics experience.
Read more about Allen Yu

Claire Chung
Claire Chung
author image
Claire Chung

Claire Chung is pursuing her PhD degree as a Bioinformatician at the Chinese University of Hong Kong. She enjoys using Python daily for work and lifehack. While passionate in science, her challenge-loving character motivates her to go beyond data analytics. She has participated in web development projects, as well as developed skills in graphic design and multilingual translation. She led the Campus Network Support Team in college, and shared her experience in data visualization in PyCon HK 2017.
Read more about Claire Chung

Aldrin Yim
Aldrin Yim
author image
Aldrin Yim

Aldrin Yim is a PhD candidate and Markey Scholar in the Computation and System Biology program at Washington University, School of Medicine. His research focuses on applying big data analytics and machine learning approaches in studying neurological diseases and cancer. He is also the founding CEO of Codex Genetics Limited, which provides precision medicine solutions to patients and hospitals in Asia.
Read more about Aldrin Yim

View More author details
Right arrow

Chapter 2. Figure Aesthetics

Now that you have entered the world of Matplotlib, surely you will want more than plain boring figures that look all the same. This chapter talks about the aesthetics of a figure. We introduce the structure and components of a Matplotlib figure and how to style these details. In this chapter, you will learn how to make a figure stylish, professional, and genuinely yours.

Here are the topics covered in this chapter:

  • Basic structure and terminologies of a Matplotlib figure
  • Setting colors in Matplotlib
  • Adjusting text formats
  • Lines and markers
    • Customizing line styles
    • Customizing marker styles
  • Grids, ticks, and axes
    • Adding and adjusting grid lines
    • Adjusting tick spacing
    • Customizing tick formatters
    • Adding axes labeling
    • Nonlinear axes
  • Title and legends
  • Style sheet support

Basic structure of a Matplotlib figure


A basic Matplotlib figure is made up of multiple components common to different plot types. It will be useful to familiarize ourselves with the terminologies, as we will be using them frequently in plotting. To get you up to speed, we have prepared a glossary of these basic objects. For clearer illustration, here is a plot adapted from Matplotlib's official website that nicely highlights the anatomy of a typical Matplotlib figure:

Glossary of objects in a Matplotlib figure

  • Figure: A figure is the whole plotting area that contains all plot elements. Multiple subplots may be tiled in grid within one figure.
  • Subplot: A subplot is a subregion in a figure that contains all of the relevant data to be displayed on the same axes. We will demonstrate how to create subplots in Chapter 3, Figure Layout and Annotations.
  • Axis: An axis measures the value of a point at a certain position. Most plots contain two axes, x (horizontal) and y (vertical). Sometimes, multiple...

Setting colors in Matplotlib


Many elements in a Matplotlib figure can have their colors specified. There are several ways to do so. You will come across the color parameter as a keyword argument for style settings very often in different functions. The alternate abbreviated keyword c can often be used. We will first briefly introduce the general rule here.

Single letters for basic built-in colors

There is a list of common colors we can quickly call with single letters:

  • b: Blue
  • g: Green
  • r: Red
  • c: Cyan
  • m: Magenta
  • y: Yellow
  • k: Black
  • w: White

Names of standard HTML colors

Examples are coralgoldspringgreendeepskyblue, and blueviolet. You can find the full list here: https://matplotlib.org/examples/color/named_colors.html.

RGB or RGBA color code

You can parse a tuple of three to four float numbers in the range of 0-1, such as (0.2,0.4,0.8) or (0.2,0.2,0.3,0.8). The first three numbers are RGB values that define the amount of red, green, and blue light to mix in the color. The optional fourth number is...

Adjusting text formats


For an informative figure, we typically have a number of text elements, including the title, labels of axes and ticks, legend, and any additional annotations. We can adjust the font size and font family in the default rc settings. These settings are set in a dictionary-like variable, matplotlib.rcParams, so you can do import matplotlib and define a parameter like this:

matplotlib.rcParams['font.size'] = 18 

Matplotlib also provides functions to alter the settings. The matplotlib.rc() changes the parameters one by one, whereas matplotlib.rcParams.update() accepts a dictionary input to change multiple settings simultaneously. Let's say we would like to change the font size to 20 and font family to serif, then use. We can do so in two ways:

matplotlib.rc('font', size=18)
matplotlib.rc('font', family='sans-serif')

This is equivalent to the following:

matplotlib.rcParams.update({'font.size': 18, 'font.family': 'serif'})

Font

Text with well-tuned fonts helps the data speak with...

Customizing lines and markers


Lines and markers are key components found among various plots. Many times, we may want to customize their appearance to better distinguish different datasets or for better or more consistent styling. Whereas markers are mainly used to show data, such as line plots and scatter plots, lines are involved in various components, such as grids, axes, and box outlines. Like text properties, we can easily apply similar settings for different line or marker objects with the same method.

Lines

Most lines in Matplotlib are drawn with the lines  class, including the ones that display the data and those setting area boundaries. Their style can be adjusted by altering parameters in lines.Line2D. We usually set colorlinestyle, and linewidth as keyword arguments. These can be written in shorthand as cls, and lw respectively. In the case of simple line graphs, we learned in Chapter 1, Hello Plotting World! that these parameters can be parsed to the plt.plot() function:

import...

Customizing grids, ticks, and axes


Lines of grids, ticks, and axes help us to visually locate and measure the data values. Their distribution and style determine whether they make good visual aids for the plot or clutter the figure. We will demonstrate the basic methods here.

Grids

Sometimes it may not be easy to tell the coordinates of any point in the plot. Grid lines extend from axis tick marks and help us estimate the value at a certain position.

Adding grids

Grids can be added by calling pyplot.grid(). By default, grid lines will be added at major tick marks. As in other line features, pyplot.grid() takes in parameters such as linewidth (lw), linestyle (ls), and color (c):

import numpy as np
import matplotlib.pyplot as plt

# Prepare 100 evenly spaced numbers from 0 to 200
evens = np.linspace(0,200,100)

# Plot a square curve
plt.plot(evens,evens**2,label = 'x^2')

# Adding grid lines
plt.grid()

plt.legend()
plt.show()

Here you see the default grid lines extending from axis ticks:

In the...

Using style sheets


We have learned to set the style details step by step so far. The matplotlib.style module provides a handy way to apply a predefined global style to the whole figure. There are a number of built-in style sheets coming along the matplotlib package. You can call matplotlib.style.available to check them out:

The function returns a list of built-in style sheets, including classic, seaborn, and ggplot. Classic refers to the Matplotlib style before version 2.0. Seaborn is a popular package built on top of Matplotlib that offers some special plotting APIs and themes for generating aesthetically attractive statistical figures. In Matplotlib 2.0, we can easily work on the styling natively for simple plot types, without importing an extra module. Multiple style sheets for different purposes are available. The ggplot style emulates ggplot_, a popular plotting package in R that features vibrant colors on a gray data area with white lines.

Applying a style sheet

Using a style is as simple...

Title and legend


Title and legend are pieces of text that facilitate quick comprehension of the data context. Although a title is not required or recommended, sometimes, such as in inline figures of many scientific publications, adding a title for your plot often helps make the message clear, especially when your figure is not accompanied by explanatory text. For plots with multiple datasets, it is a good practice to keep a data legend with a distinct color or pattern code labeled with the corresponding identities.

Adding a title to your figure

The title of a figure can be set by pyplot.title() or axes.set_title(). Text properties can be supplied as keyword arguments.

Adding a legend

Adding a legend of data labels in Matplotlib is as simple as setting label='yourlabel' when plotting and adding pyplot.legend() before pyplot.show(). By default, Matplotlib finds the "best" location to prevent the legend from overlapping with data. You may also specify a location using pyplot.legend(loc='3', **kwargs...

Test your skills


Now that we have gone through each style setting one by one, it's your showtime to combine all the techniques!

import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np 
import matplotlib as mpl

mpl.style.use('seaborn-darkgrid')

# 2001-2015 per genome sequencing cost in USD
# Adapted from NIH National Human Genome Research Institute figures
# genome.gov/sequencingcosts
# Data were quoted from builtwith.com on May 8th 2017
# Seasonal data were averaged by year for simplicity

# Prepare the data
years = list(range(2001,2016))
y_pos = np.arange(len(years)) 
seqcost = [95263071.92,70175437.42,61448421.50,53751684.08,
40157554.23,28780376.21,\
20442576.14,19934345.74,18519312.16,17534969.56,16159699.44,
16180224.10,\
13801124.19,12585658.90,11732534.52,11455315.22,10474556.36,
9408738.91,\
9047002.97,8927342.14,7147571.39,3063819.99,1352982.23,752079.90,
342502.06,\
232735.44,154713.60,108065.14,70333.33,46774.27,31512.04,31124.96,
29091.73,\
20962.78,16712.01,10496...

Summary


In this chapter, you learned how to set the styles, including colors, sizes, and shapes, of various elements in a Matplotlib plot. Now you are also aware of some stylistic considerations for a refined figure.

In the next chapter, we will continue to discuss the appearance of plots, moving our focus to the layout and annotation.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Matplotlib 2.x By Example
Published in: Aug 2017Publisher: PacktISBN-13: 9781788295260
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 (3)

author image
Allen Yu

Allen Yu, PhD, is a Chevening Scholar, 2017-18, and an MSC student in computer science at the University of Oxford. He holds a PhD degree in Biochemistry from the Chinese University of Hong Kong, and he has used Python and Matplotlib extensively during his 10 years of bioinformatics experience.
Read more about Allen Yu

author image
Claire Chung

Claire Chung is pursuing her PhD degree as a Bioinformatician at the Chinese University of Hong Kong. She enjoys using Python daily for work and lifehack. While passionate in science, her challenge-loving character motivates her to go beyond data analytics. She has participated in web development projects, as well as developed skills in graphic design and multilingual translation. She led the Campus Network Support Team in college, and shared her experience in data visualization in PyCon HK 2017.
Read more about Claire Chung

author image
Aldrin Yim

Aldrin Yim is a PhD candidate and Markey Scholar in the Computation and System Biology program at Washington University, School of Medicine. His research focuses on applying big data analytics and machine learning approaches in studying neurological diseases and cancer. He is also the founding CEO of Codex Genetics Limited, which provides precision medicine solutions to patients and hospitals in Asia.
Read more about Aldrin Yim