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.
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:
figureobject: the overall figure space that can contain one or more plots.
axisobjects: 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.
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
fig, ax = plt.subplots()
Notice that the
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,
ax are variable names for the figure and axis objects. You can call these items whatever you want. For example, you might see
# Create figure and one plot (axis object) fig, ax = plt.subplots()
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))
Using matplotlib’s object-oriented approach makes it easier to include more than one plot in a figure by creating additional
When adding more than one
axis object, it is good practice to give them distinct names (such as
ax2), so you can easily work with each
You will need to provide new arguments to
plt.subplots for the layout of the figure: number of rows and columns:
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))
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))
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 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))
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.