Lesson 5. Customize matplotlib plots in Python - earth analytics - data science for scientists


In this tutorial, you will explore more advanced plotting techniques using matplotlib.

Learning Objectives

At the end of this activity, you will be able to:

  • Use the matplotlib plot function to create custom plots.
  • Add labels to x and y axes and a title to your matplotlib plot.
  • Customize the colors and look of a matplotlib plot.

What You Need

You need Python 3.x and Jupyter notebooks to complete this tutorial. Also you should have an earth-analytics directory setup on your computer with a /data directory with it.

Download Colorado Flood Teaching Data Subset data

Important - Data Organization

Before you begin this lesson series, be sure that you’ve downloaded the data for this week of the course. After you download the data, unzip it and make sure that is in your earth-analytics/data/colorado-flood/ directory on your computer. Your directory should look like the image below: note that all of the data are within the week-02 directory. They are not nested within another directory.

week 2 file organization
Your `week_02` file directory should look like the one above. Note that the data directly under the week_02 folder.

In your week 1 homework, you used the pandas plot function to plot your data. In this tutorial, you’ll explore more advanced plotting techniques using matplotlib.

First, explore the code below to that was used to download the data and then create a quick pandas plot.

Be sure to set your working directory

os.chdir("path-to-you-dir-here/earth-analytics/data")

# import required python packages
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
import os
import urllib
plt.ion()
import earthpy as et
os.chdir(os.path.join(et.io.HOME, 'earth-analytics'))

# set parameters so all plots are consistent
plt.rcParams['figure.figsize'] = (8, 8)
plt.rcParams['axes.titlesize'] = 20
#plt.rcParams['axes.facecolor']='white'
#plt.rcParams['grid.color'] = 'grey'
#plt.rcParams['grid.linestyle'] = '-'
#plt.rcParams['grid.linewidth'] = '.5'
plt.rcParams['lines.color'] = 'purple'
#plt.rcParams['axes.grid'] = True

In the previous lessons you downloaded and imported data from figshare into Python using the following code.

# download file from Earth Lab figshare repository
urllib.request.urlretrieve(url='https://ndownloader.figshare.com/files/7010681', 
                           filename= 'data/colorado-flood/boulder-precip.csv')

# is parse dates a function argument that recoganizes dates  
boulder_precip = pd.read_csv('data/colorado-flood/downloads/boulder-precip.csv', 
                             index_col=['DATE'], 
                             parse_dates=['DATE'])

# view first few rows of data
boulder_precip.head()
Unnamed: 0PRECIP
DATE
2013-08-217560.1
2013-08-267570.1
2013-08-277580.1
2013-09-017590.0
2013-09-097600.1

Then the data was plotted using the pandas function, .plot().

# plot using pandas .plot()
boulder_precip.plot('DATE', 'PRECIP');
Basic matplotlib plot with dates on the x axis using .plot().
Basic matplotlib plot with dates on the x axis using .plot().

Plot with matplotlib

Matplotlib is a plotting package that makes it simple to create complex plots from data in a data.frame. It uses default settings, which help to create publication quality plots with a minimal amount of settings and tweaking. Matplotlib graphics are built step by step by adding new elements.

To build a matplotlib plot you need to:

  1. define the axes
# set plot formatting for all plots in the notebook
plt.rcParams['figure.figsize'] = (8, 8)
plt.rcParams['axes.titlesize'] = 20
#plt.rcParams['axes.facecolor']='white'
#plt.rcParams['grid.color'] = 'grey'
#plt.rcParams['grid.linestyle'] = '-'
#plt.rcParams['grid.linewidth'] = '.5'
#plt.rcParams['axes.grid'] = True
# first create the plot space upon which to plot the data
fig, ax = plt.subplots()
When you create a figure object you are creating a blank canvas to place a plot on.
When you create a figure object you are creating a blank canvas to place a plot on.
  1. Define the plot elements including the x and y variables and the data to be used

ax.plot(boulder_precip['DATE'], boulder_precip)['PRECIP'])

fig, ax = plt.subplots()
ax.plot(boulder_precip['DATE'], 
        boulder_precip['PRECIP'], 
        color = 'purple');
Once you add a call to plot using ax.plot - your blank canvas has a plot on it.
Once you add a call to plot using ax.plot - your blank canvas has a plot on it.

Then finally you can refine your plot adding labels and other elements.

# define figure
fig, ax = plt.subplots()
# Define data and x and y axes
ax.plot(boulder_precip['DATE'], boulder_precip['PRECIP'])
# set plot title
ax.set(title="matplotlib example plot precip")
plt.setp(ax.get_xticklabels(), rotation=45);
You can customize your plot adding and adjust ticks and titles.
You can customize your plot adding and adjust ticks and titles.

You can customize the look of the plot in different ways. For instance, you can change the point marker type to a circle using the function argument: -o.

Visit the Matplotlib documentation for a list of marker types.

Marker symbolMarker description
.point
,pixel
ocircle
vtriangle_down
^triangle_up
<triangle_left
>triangle_right
fig, ax = plt.subplots()
ax.plot(boulder_precip['DATE'], boulder_precip['PRECIP'], '-o')
ax.set(title="matplotlib example plot precip")
plt.setp(ax.get_xticklabels(), rotation=45);
Here you adjust the point markers used in your plot.
Here you adjust the point markers used in your plot.
# subset your data
# dates_subset = boulder_precip['2013-09-27':'2013-10-11']
# dates_subset.head()

Data Tip: If the data you want to plot is the index of the DataFrame you can access it with DataFramename.index

Customize point colors

You can also apply a color to your points using the c= argument. Below the points are to blue using c='blue'

A list of some of the color options available in python is below:

    b: blue
    g: green
    r: red
    c: cyan
    m: magenta
    y: yellow
    k: black
    w: white

Data Tip: For more information about conda environments check out the matplotlib colors documentation

fig, ax = plt.subplots()
ax.scatter(boulder_precip.index.values,
           boulder_precip['PRECIP'].values,
           c='blue')
plt.setp(ax.get_xticklabels(), rotation=45)
plt.show()
Adjust the ticklabels on the x-axis and create a scatterplot.
Adjust the ticklabels on the x-axis and create a scatterplot.

You can adjust the transparency using the alpha= argument.

fig, ax = plt.subplots()
ax.scatter(boulder_precip.index.values, 
           boulder_precip['PRECIP'].values,
           c='blue', alpha=.5)
plt.setp(ax.get_xticklabels(), rotation=45);
Adjust the alpha value to add transparency to your points.
Adjust the alpha value to add transparency to your points.

You can also assign each point a color based upon it’s data value by

assigning c= to the values in the data. c=boulder_precip['PRECIP'].values

and then specifying colors using the cmap= argument. cmap='rainbow'

# setup figure
fig, ax = plt.subplots()
# create scatterplot
ax.scatter(boulder_precip.index.values, 
           boulder_precip['PRECIP'].values,
           c=boulder_precip['PRECIP'].values, 
           alpha=.5, cmap='rainbow')
# adjust x axis labels
plt.setp(ax.get_xticklabels(), rotation=45);
Color points according to an attribute value.
Color points according to an attribute value.

Create Bar Plots with Matplotlib

You can turn your plot into a bar plot using ax.bar().

fig, ax = plt.subplots()
ax.bar(boulder_precip['DATE'].values, boulder_precip['PRECIP'].values)
plt.setp(ax.get_xticklabels(), rotation=45);
When you create a bar plot you need to call .values on your data.
When you create a bar plot you need to call .values on your data.

Turn the bar outlines blue

fig, ax = plt.subplots()
ax.bar(boulder_precip.index.values, 
       boulder_precip['PRECIP'].values,
       edgecolor='blue')
plt.setp(ax.get_xticklabels(), rotation=45);
Just like you could adjust point colors you can adjust the bar fill and edge colors.
Just like you could adjust point colors you can adjust the bar fill and edge colors.

Change the fill to bright green.

fig, ax = plt.subplots()
ax.bar(boulder_precip['DATE'].values, boulder_precip['PRECIP'].values,
       edgecolor='blue', color='green')
plt.setp(ax.get_xticklabels(), rotation=45);
Here the bar color is set to green and the edge color is blue.
Here the bar color is set to green and the edge color is blue.

Use Matplotlib Styles or Themes

Try out a new style usind this function

Matplotlib also has a set of predefined color palettes that you can apply to a plot. To view a list of available styles, use:

plt.style.use()

# view all styles 
print(plt.style.available)
['_classic_test', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark-palette', 'seaborn-dark', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'seaborn', 'Solarize_Light2', 'tableau-colorblind10']
fig, ax = plt.subplots()
ax.bar(boulder_precip['DATE'].values, boulder_precip['PRECIP'].values)
plt.setp(ax.get_xticklabels(), rotation=45);
plt.style.use('fivethirtyeight')
plt.show()
There are many styles that you can apply to make your plots look nicer and uniform. Here a style is called within the plot code. This style will only be applied to this plot.
There are many styles that you can apply to make your plots look nicer and uniform. Here a style is called within the plot code. This style will only be applied to this plot.

Add Plot Labels

You can add labels to your plots as well. Let’s add a title, and x and y labels using the xlabel and ylabel arguments within the ax.set() function.

fig, ax = plt.subplots()
ax.bar(boulder_precip['DATE'].values, 
       boulder_precip['PRECIP'].values,
       edgecolor='blue')
plt.setp(ax.get_xticklabels(), rotation=45);
ax.set(xlabel="Date", ylabel="Precipitation (Inches)")
ax.set(title="Daily Precipitation (inches)\nBoulder, Colorado 2013");
You can label your x and y axes as well.
You can label your x and y axes as well.

Set Graph formatting for an Entire Notebook:

If you know that you want all of your graphs to have a white background, be a specific figure size and have titles, and labels the same font size the plt.rcParams function will save you some typing! This function allows you to set graph formatting for an entire notebook if it’s placed at the beginning.

plt.rcParams['figure.figsize'] = (8, 8) plt.rcParams['axes.titlesize'] = 20 plt.rcParams['axes.facecolor']='white'

figure.figuresize sets the size of the figure
axis.titlesize sets the title font size axes.facecolor sets the figure background color.

For a complete list of what you can pre-set using plt.rcParams check out the documentation

Additional Resources

Here are some additional matplotlib examples and information about color bars. Here is an in-depth guide to matplotlib that will be useful for you to refer back to.

Leave a Comment