Visualizing Data
Data visualization means showing data as a chart so patterns are easy to see. matplotlib is the standard Python library for it, and it is used to explore data, check results and explain findings to other people. A column of ten numbers is hard to read; a bar chart of the same numbers shows the busiest month at a glance.
Example: Payments by Month
This program loads the 1,000 payments from "Pandas Basics", fills missing amounts, totals them by month and saves a bar chart:
import matplotlib
matplotlib.use("Agg") # draw without opening a window
import matplotlib.pyplot as plt
import pandas as pd
payments = pd.read_csv("payments.csv")
payments["amount"] = payments["amount"].fillna(payments["item"].map(
{"tuition": 250.0, "lab": 45.0, "field trip": 30.0, "library": 15.0}))
by_month = payments.groupby("month")["amount"].sum()
plt.rcParams["svg.hashsalt"] = "poolstring" # stable SVG output
fig, ax = plt.subplots(figsize=(9, 4.5))
ax.bar(by_month.index, by_month.values, color="#1e6fd9")
ax.set_title("Fee payments by month, 2026")
ax.set_xlabel("Month")
ax.set_ylabel("Total paid (US dollars)")
ax.tick_params(axis="x", rotation=45)
fig.tight_layout()
fig.savefig("payments-by-month.svg", metadata={"Date": None})
print("Saved payments-by-month.svg")Saved payments-by-month.svgThe chart below is the exact file this program saved, shown unchanged inside the course's picture frame:
Explanation: plt.subplots() creates a figure and its axes. ax.bar() draws one bar per month, and the title and axis labels say what the chart shows and in which units. matplotlib.use("Agg") draws straight to a file without opening a window, and the two settings marked "stable" just make the saved file identical on every run.
Which Chart for Which Question
| Chart | Method | It is used to show... |
|---|---|---|
| Bar chart | ax.bar() | Amounts per category, such as payments by month |
| Line chart | ax.plot() | Change over time, such as weekly active users |
| Histogram | ax.hist() | How values are spread, such as fee sizes |
| Scatter plot | ax.scatter() | The relationship between two numbers, such as area and price |
Common Mistakes
Missing labels or units. "Total paid (US dollars)" is clearer than "amount".
Choosing a chart by looks. Choose by the question: compare, change over time, spread or relationship.
Too much on one chart. Split it into two simple charts instead.
Interview Corner
Which chart would you use to show change over time?
A line chart, with time on the x-axis. Use bar charts to compare categories, histograms for distributions and scatter plots for relationships between two numbers.
What makes a chart trustworthy?
A clear title, labeled axes with units, an honest scale (bars starting at zero) and a stated data source, so readers can interpret it correctly.
Practice
Exercise: the checkpoint chart. Recreate the chart, then add a second chart of payments by item. Which item brings in the most? Would a bar chart or a pie chart show it more clearly, and why?
Check your understanding:
- Which method draws a bar chart?
- What three things does every chart need?
- Which chart shows a relationship between two numbers?
Key Takeaways
Quick reference
| Need | Code |
|---|---|
| Figure | fig, ax = plt.subplots() |
| Bars | ax.bar(x, y) |
| Labels | ax.set_title, ax.set_xlabel, ax.set_ylabel |
| Save | fig.savefig("chart.svg") |
Was this tutorial helpful?