7. Matplotlib#

This chapter was coauthored by Jason DeBacker and Richard W. Evans.

Matplotlib is Python’s most widely used and most basic visualization package.[1] Some of the other most popular Python visualization packages Bokeh, Plotly, and Seaborn. Of these, Matplotlib is the most general for static images and is what is used on OG-Core. Once you have a general idea of how to create plots in Python, that knowlege will generalize (to varying degrees) to the other plotting packages.

The iframe below contains a PDF of the BYU ACME open-access lab entitled, “Introduction to Matplotlib”. You can either scroll through the lab on this page using the iframe window, or you can download the PDF for use on your computer. See [BYU ACME, 2023]. Exercise 7.1 below has you work through the problems in this BYU ACME lab. A Python file template (matplotlib_intro.py) and a data file (FARS.npy) used in the lab are stored in the ./code/Matplotlib1/ directory.

The iframe below contains a PDF of the BYU ACME open-access lab entitled, “Pandas 2: Plotting”. In spite of having “Pandas” in the title, we include this lab here in this Matplotlib chapter because all of the plotting uses the Matplotlib package. You can either scroll through the lab on this page using the iframe window, or you can download the PDF for use on your computer. See [BYU ACME, 2023]. Exercise 7.2 below has you work through the problems in this BYU ACME lab. A Jupyter notebook file template (matplotlib2.ipynb) used in the lab is stored in the ./code/Matplotlib2/ directory. The budget.csv and crime_data.csv data files are stored in the ./data/Pandas1 directory, which were used in the “Pandas 1: Introduction” lab. And the other data file used in this lab college.csv is stored in the ./data/Pandas3 directory, which was used in the “Pandas 3: Grouping” lab.

7.1. (Optional): Animations and 3D#

This section with its accompanying BYU ACME lab and Exercise 7.3 is optional because these plotting skills are used less often and because other plotting packages do a better job of visualization dynamics. That said, this lab is a good one. And the 3D plotting in Matplotlib is fairly good.

The iframe below contains a PDF of the BYU ACME open-access lab entitled, “Animations and 3D Plotting in Matplotlib”. You can either scroll through the lab on this page using the iframe window, or you can download the PDF for use on your computer. See [BYU ACME, 2023]. Optional Exercise 7.3 below has you work through the problems in this BYU ACME lab. A Jupyter notebook file template (animation.ipynb) used in the lab is stored in the ./code/Matplotlib3/ directory. And two data files used in the lab are stored in the ./data/Matplotlib3 directory.

7.2. Exercises#

Exercise 7.1

Read the BYU ACME “Introduction to Matplotlib” lab and complete Problems 1 through 6 in the lab. [BYU ACME, 2023]

Exercise 7.2

Read the BYU ACME “Pandas 2: Plotting” lab and complete Problems 1 through 4 in the lab. [BYU ACME, 2023]

Exercise 7.3 (OPTIONAL: Animations nad 3D)

Read the BYU ACME “Animations and 3D Plotting in Matplotlib” lab and complete Problems 1 through 5 in the lab. [BYU ACME, 2023]

Exercise 7.4

Using the country GDP DataFrame you created in Exercise Exercise 6.3, collapse these data to find mean GDP per capita by country. Create a bar plot that shows the means for each of the four countries.

Exercise 7.5

Using same DataFrame as above, create a grouped bar plot that represents the full DataFrame and shows GDP per capita for each country and year. Group the bar plot so that there is a grouping for each decade and within each group, all four countries are represented.

7.3. Footnotes#

The footnotes from this chapter.