Module 6 · Matplotlib: Charts That Explain · Lesson 19 of 51

Your first charts: figure, axes, line and bar

How Matplotlib thinks (a figure holds axes), line and bar charts, titles, labels and formatting, and choosing the right chart for the question.

In this lesson · about 40 minutes

  • Create a figure and axes with plt.subplots
  • Draw line and bar charts with a title, axis labels and readable ticks
  • Match a question to the right chart type

The pattern you'll use every time

import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(8, 4))     # width, height in inches
ax.plot(x, y)                               # or ax.bar, ax.scatter, ax.hist …
ax.set_title("Daily sales, Campus, March 2026")
ax.set_xlabel("Date")
ax.set_ylabel("Sales ($)")
plt.show()

Always work through ax (the "object-oriented" style). Shortcuts like plt.plot work too, but ax scales cleanly to several charts.

Line and bar

  • ax.plot(dates, values, marker="o", color="tab:orange", label="Campus"): change over time. Add ax.legend() when there is more than one line.
  • ax.bar(names, values) compares categories; ax.barh is horizontal, best for long names. Sort the bars so the ranking is obvious.
  • ax.bar_label(bars, fmt="%.0f") writes the value on each bar.
  • ax.tick_params(axis="x", rotation=45) tilts crowded labels; ax.grid(axis="y", alpha=0.3) adds light guide lines.

Which chart answers which question?

QuestionChart
Compare categoriesBar (sorted)
Change over timeLine
Relationship between two numbersScatter
Spread / shape of one numberHistogram, box plot
Parts of a whole (2–4 parts)Stacked bar, or pie with care

Try it · data lab

Pick the right chart

Five real questions about the café. For each, try different chart types and see which one answers it best, and how a bar chart can mislead.

Chart
Downtown: 42.7%Airport: 24.3%Campus: 20.5%Riverside: 12.5%
Acceptable, but not the best. Comparing a few categories is a bar chart's job: lengths are easy to compare by eye. A pie works for parts of a whole but makes close values hard to tell apart.

Monthly revenue: a line and a bar

Exercise

Bar chart: orders per payment method

counts holds the number of orders per payment method, sorted. Draw it as a vertical bar chart on ax, with the title Orders by payment method and the y-axis label Orders.

Code that runs before yours
import pandas as pd
import matplotlib.pyplot as plt
orders = pd.read_csv("kc_orders.csv", parse_dates=["date"])
counts = orders["payment"].value_counts()

Exercise

Line chart: Downtown's daily sales in March

dt holds Downtown's rows from the daily table for March. Plot dt["date"] against dt["sales"] as a line on ax, and set the title to Downtown daily sales, March 2026.

Code that runs before yours
import pandas as pd
import matplotlib.pyplot as plt
daily = pd.read_csv("kc_daily.csv", parse_dates=["date"])
dt = daily[(daily["branch"] == "Downtown") & (daily["date"].dt.month == 3)]

Check your understanding0 of 3 correct

Question 1

1.In fig, ax = plt.subplots(), what is ax?

Question 2

2.Best chart for 'revenue in each of 4 branches'?

Question 3

3.Long category names overlap on the x-axis. Simplest fix?

Quiz score: 0 of 3

Exercises solved: 0 of 2

Tip: answer every quiz question correctly first.

Written by Noviq Academy in its own words. Kettle & Crumb and all its data are made up for teaching; any resemblance to a real business is a coincidence. Python runs in your browser with Pyodide, so results can differ slightly from your own computer. Power BI, Tableau and other product names belong to their owners. Read-aloud uses your device's built-in voices.