Visualizing Data

Updated · 4 min read
easyPython 3.13aipythonmatplotlibchartsdata-visualization

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:

Python
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")
Output
Saved payments-by-month.svg

The chart below is the exact file this program saved, shown unchanged inside the course's picture frame:

The real chart from this lesson's code

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

ChartMethodIt is used to show...
Bar chartax.bar()Amounts per category, such as payments by month
Line chartax.plot()Change over time, such as weekly active users
Histogramax.hist()How values are spread, such as fee sizes
Scatter plotax.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:

  1. Which method draws a bar chart?
  2. What three things does every chart need?
  3. Which chart shows a relationship between two numbers?

Quick check: visualizing data

4 questions
1Which matplotlib method draws bars?
2Which month has the tallest bar?
3Which chart shows change over time?
4Why start a bar chart's y-axis at zero?

Key Takeaways

Quick reference

NeedCode
Figurefig, ax = plt.subplots()
Barsax.bar(x, y)
Labelsax.set_title, ax.set_xlabel, ax.set_ylabel
Savefig.savefig("chart.svg")

Was this tutorial helpful?

Lesson 80 of 186