lesson
45 min

Choosing the Right Chart

Each chart type answers one kind of question. Learn which is which, and build them in Python.

Objective

By the end of this lesson, you can choose a chart type that fits your question and build it with matplotlib.

Look before you model

Charts are how engineers find problems before a model does. A single chart can show a broken sensor, a typo that turned 70 into 700, or a gap where a year of data is missing. Always chart your data first.

Match the chart to the question

QuestionChart
How does something change over time?Line chart
How do groups compare?Bar chart (sorted)
Do two numbers move together?Scatter plot
How are values spread out?Histogram

Pie charts are hard to read when slices are close in size. A sorted bar chart almost always works better.

A chart with no library

This draws a bar chart out of text characters. It runs in your browser.

Run it. Each # stands for 10 days. Sort the regions from most to fewest days, then run it again. Sorted bars are easier to compare.
Python 3
Ctrl+Enter to run | Tab to indent | Python runs in your browser via Pyodide

Real charts with matplotlib

matplotlib is the standard Python charting library. Run this in Google Colab:

import matplotlib.pyplot as plt

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
avg_high_f = [48, 52, 61, 70, 79, 87]   # made-up monthly averages

plt.figure(figsize=(6, 3))
plt.plot(months, avg_high_f, marker="o")
plt.title("Average high temperature by month (sample data)")
plt.ylabel("Degrees Fahrenheit")
plt.tight_layout()
plt.show()

Swap plt.plot for plt.bar (groups), plt.scatter(x, y) (two numbers), or plt.hist(values) (spread).

A national example

Quiz: Pick the chart

  1. 01You want to show how electricity demand changed hour by hour over a week. Which chart?

    Choose one.

  2. 02You want to see if county income and internet access move together. Which chart?

    Choose one.

  3. 03Why chart data before modeling?

    Choose one.

Ask. Answer. Build together.