Welcome to this lesson on matplotlib! As we just learned, matplotlib is a Python library used for making graphs. It’s commonly used by research scientists and data scientists, among other professionals. Matplotlib makes it easy to turn tabular data (data in a spreadsheet, table, or .csv) into basic visualizations.
In this lesson we will…
- Learn about five basic chart types
- Review the general structure of matplotlib code
- Make a line chart to compare temperature data from around the world
- Explore the line chart: change style options, adjust axes, and add title and legend
In the Jupyter notebook to the right, we’ll start by importing matplotlib.pyplot
as plt
, and importing a dataset of average monthly temperatures around the world.
Instructions
Let’s practice running the first two Jupyter cells to load our packages and display the chart correctly.
- Select the code cell beginning
# load matplotlib and pandas
(click anywhere in the cell) - Click the
Run
button or theShift
+Enter/Return
keys (see image below) - Click the
Save
button or use theControl/Command
+S
keys to save your work - Repeat this process for the cell that says
# display charts correctly
. - Click the
Test Work
button below the Jupyter Notebook to check if you’ve completed the Checkpoint!
If you’ve successfully completed the Checkpoint, you’ll get a green check at the top of the checkpoint. When all checkpoints are complete, the Next
button at the bottom right will become clickable.
We’ll need some data to get a data visualization going! We’re going to look at monthly average temperatures from around the world. Run the cell that says ## load in the data
, then Save the notebook and click Test Work. Refer back to the steps in Checkpoint 1 if needed.
With our data loaded, let’s take a peek at what matplotlib can do. The next code block is all set up: still no need to type anything, just Run the cell that says # make a line chart
, hit Save, then Test Work. Once you’re done, you can click Next
to move on to the next exercise.