In this lesson, you will write
Python code to plot data from lists using the
After completing this lesson, you will be able to:
Pythoncode to plot data from lists using the
Pythoncode to customize your plots (e.g. titles, axes labels, colors)
What You Need
The code below is available in the ea-bootcamp-day-2 repository that you cloned to
earth-analytics-bootcamp under your home directory.
Types of Plots
Plots are very useful for displaying information that has a temporal occurrence such as the monthly precipitation data from the previous lessons. There are many different kinds of plots including line and bar graphs as well as scatter plots (i.e. plots of points representing observations in the data).
To create a plot, you need to provide data for the x-axis (i.e. horizontal axis) and the y-axis (i.e. vertical axis) of the plot. The data can be contained in various formats including lists and other data structures that you will work with in this course such as
numpy arrays and
In this lesson, you will use your previously created lists for average monthly precipitation in Boulder, CO to create and customize a plot. You will use
months along the x-axis and
precip along the y-axis.
Begin Writing Your Code
Now that you know how to import
Python packages, you can begin your code by importing the
matplotlib package, and specifically, the
Recall that in the
Python (that you are now a part of!), the
matplotlib.pyplot is often assigned an alias of
# import necessary Python packages import matplotlib.pyplot as plt # print a message after the package has been imported successfully print("import of packages successful")
import of packages successful
Python skills to create lists of the converted values for average monthly precipitation (mm) and of the month names.
# create variables for each month of average precipitation for Boulder, CO jan = 0.70 * 25.4 feb = 0.75 * 25.4 mar = 1.85 * 25.4 apr = 2.93 * 25.4 may = 3.05 * 25.4 june = 2.02 * 25.4 july = 1.93 * 25.4 aug = 1.62 * 25.4 sept = 1.84 * 25.4 oct = 1.31 * 25.4 nov = 1.39 * 25.4 dec = 0.84 * 25.4 # create a list of the converted monthly variables for the y-axis of your plot precip = [jan, feb, mar, apr, may, june, july, aug, sept, oct, nov, dec] # create a list of the month names for the x-axis of your plot months = ["Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"]
Plot Data From Lists
Since you now have lists containing the converted values for average monthly precipitation and the month names, you can use these lists to create a plot using
Matplotlib is a plotting package that makes it simple to create plots from various data structures in
Python, including lists.
Matplotlib 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:
- Create the empty plot on which data will be plotted
- Define the plot elements including the x- and y- axes (variables)
- Customize your plot to change default styles and to add titles and labels to the axes.
Begin by creating a plot using the default styles provided by
# set plot size for the plot plt.rcParams["figure.figsize"] = (8, 8) # create the plot space upon which to plot the data fig, ax = plt.subplots() # add the x-axis and the y-axis to the plot ax.plot(months, precip);
Customize Your Plot
Add Title and Axis Labels
Expand your code to add a title to the plot and to label the axes.
# set plot size for all plots that follow plt.rcParams["figure.figsize"] = (8, 8) # set plot title size for all plots that follow plt.rcParams['axes.titlesize'] = 20 # create the plot space upon which to plot the data fig, ax = plt.subplots() # add the x-axis and the y-axis to the plot ax.plot(months, precip) # set plot title ax.set(title="Average Monthly Precipitation in Boulder, CO") # add labels to the axes ax.set(xlabel="Month", ylabel="Precipitation (mm)");
Change Plot Type
You can turn your plot into a bar plot using
ax.bar(), providing the x- and y-axes as you did with
You can also assign a fill color using
# set plot size for all plots that follow plt.rcParams["figure.figsize"] = (8, 8) # create the plot space upon which to plot the data fig, ax = plt.subplots() # add the x-axis and the y-axis to the plot ax.bar(months, precip, color="green") # set plot title ax.set(title="Average Monthly Precipitation in Boulder, CO") # add labels to the axes ax.set(xlabel="Month", ylabel="Precipitation (mm)");
Congratulations - you have created your first customized plots of data!
Python skills to further customize your plot:
Recreate the x-axis to use full month name (e.g.
Jan) (hint: create a new
Rotate the x-axis markers using
plt.setp(ax.get_xticklabels(), rotation=45), so that the spacing along the x-axis is more appealing now that the months are longer.
Change your plot to a scatter plot using