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
| Question | Chart |
|---|---|
| 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.
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
Discussion
Ask. Answer. Build together.
Loading…