# 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.

## 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.

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

# is parse dates a function argument that recoganizes dates
index_col=['DATE'],
parse_dates=['DATE'])

# view first few rows of data

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');


## 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()

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');


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 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);

# subset your data
# dates_subset = boulder_precip['2013-09-27':'2013-10-11']


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()


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);


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')
plt.setp(ax.get_xticklabels(), rotation=45);


### 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);


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);


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);


### 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()


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)")


### 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

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.