Module 07 / Project 04 — Visualization¶
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| — | This project | — | — | Flashcards | — | — |
Focus¶
- matplotlib basics:
figure(),plot(),bar(),scatter() - Adding titles, axis labels, and legends
- Creating multi-panel figures with
subplots() - Saving figures to files with
savefig() - Using the Agg backend for non-interactive environments
Why this project exists¶
Numbers in a table are hard to interpret. A chart makes patterns, outliers, and trends visible at a glance. This project teaches you how to create four common chart types with matplotlib — the standard Python plotting library. You will also learn how to combine multiple charts into a single figure and save the result to a file, which is how charts are shared in reports and presentations.
Run¶
Expected output¶
=== Loading student data ===
Loaded 30 rows from data/students.csv
=== Chart 1: Bar chart — Average grade by subject ===
Created bar chart.
=== Chart 2: Line chart — Grades over student index ===
Created line chart.
=== Chart 3: Scatter plot — Age vs grade ===
Created scatter plot.
=== Chart 4: Combined 2x2 subplot ===
Saved combined figure to data/charts.png
Done. Open data/charts.png to see all four charts.
After running, open data/charts.png to see the four charts arranged in a 2x2 grid.
Alter it¶
- Change the bar chart colors. Try
color="coral"orcolor=["#e74c3c", "#3498db", "#2ecc71"]. - Add a horizontal line to the line chart at the class average:
ax.axhline(y=mean_grade, color="red", linestyle="--"). - Change the scatter plot to use different colors per subject. (Hint: loop through subjects and plot each one separately with a label.)
- Add a histogram of all grades as a fifth chart. Change the subplot grid to 2x3 or 3x2.
Break it¶
- Remove the
matplotlib.use("Agg")line and run on a machine with no display. What error do you get? - Try creating a subplot grid of 2x2 but only fill 3 panels. What does the empty panel look like?
- Pass a non-existent column name to the plot function. Read the error.
Fix it¶
- Always set the backend before importing pyplot:
matplotlib.use("Agg")must come beforeimport matplotlib.pyplot as plt. - Hide unused subplot panels with
ax.set_visible(False). - Check that columns exist before plotting:
if column_name in df.columns.
Explain it¶
- What is the difference between
plt.plot()andax.plot()? When would you use each? - What does
matplotlib.use("Agg")do? Why is it needed in scripts that save to files? - What is a "figure" vs an "axes" in matplotlib? How do they relate?
- Why does
savefig()need to be called beforeplt.show()?
Mastery check¶
You can move on when you can:
- Create a bar chart, line chart, and scatter plot from a DataFrame.
- Add titles, axis labels, and a legend to any chart.
- Create a multi-panel figure with
subplots(). - Save a figure to a PNG file.
- Explain the difference between figure, axes, and plot.
Related Concepts¶
- Collections Explained
- Files and Paths
- How Loops Work
- Types and Conversions
- Quiz: Collections Explained