Lesson 1. Introduction to Plotting in Python Using Matplotlib


Introduction to Plotting with Matplotlib - Scientists guide to plotting data in python textbook course module

Welcome to the first lesson in the Introduction to Plotting with Matplotlib module. Matplotlib is the most commonly used plotting library in Python. Learn how to get started with creating and customizing plots using matplotlib.

Chapter One - Introduction to Plotting with Matplotlib

In this chapter, you will learn how to create and customize plots in Python using matplotlib, including how to create different types of plots and customize plot colors and labels.

Learning Objectives

After completing this chapter, you will be able to:

  • Explain the matplotlib object-oriented approach to plotting.
  • Use matplotlib to create scatter, line and bar plots.
  • Customize the labels, colors and look of your matplotlib plot.
  • Create figures with multiple plots.
  • Save figure as an image file (e.g. .png format).

What You Need

Be sure that you have followed the instructions on Setting up Git, Bash, and Conda on your computer to install the tools for your operating system (Windows, Mac, Linux).

Be sure that you have completed the chapter on Jupyter Notebook.

Overview of Plotting with Matplotlib

Matplotlib is a Python plotting package that makes it simple to create two-dimensional plots from data stored in a variety of data structures including lists, numpy arrays, and pandas dataframes.

Matplotlib uses an object oriented approach to plotting. This means that plots can be built step-by-step by adding new elements to the plot.

There are two primary objects associated with a matplotlib plot:

  • figure object: the overall figure space that can contain one or more plots.
  • axis objects: the individual plots that are rendered within the figure.

You can think of the figure object as your plot canvas. You can think about the axis object as an individual plot.

A figure can hold one or more axis objects. This structure allows you to create figures with one or more plots on them.

A figure created using matplotlib can contain one or many plots, or axis objects. Source: Earth Lab, Alana Faller
A figure created using matplotlib can contain one or many plots, or axis objects. Source: Earth Lab, Alana Faller

While Matplotlib contains many modules that provide different plotting functionality, the most commonly used module is pyplot.

Pyplot provides methods that can be used to add different components to figure objects, including creating the individual plots as axis objects, also known as subplots.

The pyplot module is typically imported using the alias plt as demonstrated below.

# Import pyplot
import matplotlib.pyplot as plt

Create Plots Using Matplotlib

To create a plot using matplotlib’s object oriented approach, you first create the figure (which you can call fig) and at least one axis (which you can call ax) using the subplots() function from the pyplot module:

fig, ax = plt.subplots()

Notice that the fig and ax are created at the same time by setting them equal to the output of the pyplot.subplots() function. As no other arguments have been provided, the result is a figure with one plot that is empty but ready for data.

Data Tip: In the example above, fig and ax are variable names for the figure and axis objects. You can call these items whatever you want. For example, you might see f, ax or fig, axis1 used.

# Create figure and one plot (axis object) 
fig, ax = plt.subplots()
When you create a figure object, you are creating a blank canvas upon which to place a plot.
When you create a figure object, you are creating a blank canvas upon which to place a plot.

Change Figure Size

You can change the size of your figure using the argument figsize to specify a width and height for your figure:

figsize = (width, height)

# Resize figure
fig, ax = plt.subplots(figsize = (10, 6))
When you create a figure object, you can define the figure size by providing a width and height value (width, height).
When you create a figure object, you can define the figure size by providing a width and height value (width, height).

Multi-plot Figures

Using matplotlib’s object-oriented approach makes it easier to include more than one plot in a figure by creating additional axis objects.

A figure created using matplotlib can contain many plots, or axis objects. Source: Earth Lab, Alana Faller
A figure created using matplotlib can contain many plots, or axis objects. Source: Earth Lab, Alana Faller
When creating a figure with multiple axis objects, you can arrange the plots across multiple rows and columns. Source: Earth Lab, Alana Faller
When creating a figure with multiple axis objects, you can arrange the plots across multiple rows and columns. Source: Earth Lab, Alana Faller

When adding more than one axis object, it is good practice to give them distinct names (such as ax1 and ax2), so you can easily work with each axis individually.

You will need to provide new arguments to plt.subplots for the layout of the figure: number of rows and columns:

plt.subplots(1, 2)

In this example, 1, 2 indicates that you want the plot layout to be 1 row across 2 columns.

# Figure with two plots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (10, 6))
You can create figures with multiple plots by adding additional axis objects (e.g. ax1, ax2).
You can create figures with multiple plots by adding additional axis objects (e.g. ax1, ax2).

Conversely, 2, 1 indicates that you want the plot layout to be 2 rows across one column.

# Figure with two plots
fig, (ax1, ax2) = plt.subplots(2, 1, figsize = (10, 6))
You can change the layout of the figure to specify how many rows and columns you want to use to display the plots.
You can change the layout of the figure to specify how many rows and columns you want to use to display the plots.

Because you have defined figsize=(10, 6), the figure space remains the same size regardless of how many rows or columns you request.

You can play around with both the number of rows and columns as well as figsize to arrive at the look that you want.

# Figure with two plots
fig, (ax1, ax2) = plt.subplots(2, 1, figsize = (12, 12))
You can adjust the figshare as well as the number of rows and columns for a figure layout, in order to achieve the desired layout of the plots.
You can adjust the figshare as well as the number of rows and columns for a figure layout, in order to achieve the desired layout of the plots.

You can continue to add as many as axis objects as you need to create the overall layout of the desired figure and continue adjusting the figsize as needed.

# Figure with three plots
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize = (15, 15))
You can continue to add multiple plots to your figure by adding additional axis objects (e.g. ax1, ax2, ax3).
You can continue to add multiple plots to your figure by adding additional axis objects (e.g. ax1, ax2, ax3).

A key benefit of the matplotlib object oriented approach is that each axis is its own object and can be customized independently of the other plots in the figure.

You will learn how to take advantage of this capability to customize individual plots on the next page of this chapter.

Leave a Comment