Lesson 05 · Data Science → AI/ML Engineering

Charts: line and bar

A table of monthly averages is hard to read at a glance, and a chart makes the trend obvious. This lesson draws the two charts you'll use most: a line for change over time, and bars for comparing groups.

≈ 15 minLearn → Practice → ReviewBuilds on: Lesson 04

Twelve reviews of three products, January to June. As in Lesson 04, date arrives as text. pd, plt and df are already loaded.

plt is matplotlib, Python's standard drawing library, loaded with import matplotlib.pyplot as plt. Like pd, plt is just the nickname everyone uses. You'll mostly draw with pandas' .plot() method, which you call on a Series and which uses matplotlib for you. plt is the general tool for touching up a chart afterwards.

productdatestars
0Purifier2026-01-065
1Heater2026-01-194
2Dryer2026-02-024
3Purifier2026-02-144
4Heater2026-02-275
5Purifier2026-03-052
6Dryer2026-03-213
7Purifier2026-04-082
8Heater2026-04-254
9Dryer2026-05-113
10Purifier2026-05-301
11Heater2026-06-165

Part 1 · Learn

The syntax, and what each piece draws

AA line chart of a trend

SyntaxDraws
monthly.plot()A line chart of a Series: its index goes along the bottom (x), its values go up the side (y). Here that's months along the bottom and average stars up the side.
what it returnsAn Axes: the chart area itself. You can ignore it for now; section D uses it.

You don't build the chart from scratch. You make the Series you want to see (as in Lesson 04), then call .plot() on it. If the chart looks wrong, the problem is almost always the Series, so print it first.

In this page (and in Jupyter) the chart appears under the code. Each Run starts with a blank chart, so put every line that draws on the same chart in one block.

BA bar chart to compare groups

SyntaxDraws
s.plot.bar()One upright bar per label in the index, in the Series' order. Same as s.plot(kind="bar").
s.plot.barh()Horizontal bars. Easier to read when the labels are long. The first label is drawn at the bottom.
s.sort_values(ascending=False).plot.bar()Bars ranked biggest first. Sort the Series before plotting; the chart keeps whatever order it's given.

Which chart? Use a line when the x-axis is time, because the line between two months says "it changed from here to here". Use bars for separate groups like products. A line from Dryer to Heater would be meaningless, since nothing lies "between" two products.

CTitles, labels and an honest axis

SyntaxDoes
.plot(title="…")A title above the chart. Works on .plot.bar(…) and .plot.barh(…) too.
.plot(xlabel="…", ylabel="…")Names the bottom (x) and side (y) axes. pandas names x after the index (here date or product) and leaves y blank.
.plot(ylim=(0, 5))Fixes the y-axis to run from 0 to 5, instead of letting it zoom in on your data.
plt.title("…"), plt.ylabel("…"), plt.ylim(0, 5)The same changes made with matplotlib, on a line after the chart is drawn.

Why fix the axis? Left alone, the y-axis zooms in on your data, so a drop from 4.3 to 4.1 stars can fill the whole chart and look like a crash. For star ratings, ylim=(0, 5) or (1, 5) shows the real scale. Bar charts should always start at 0, because people compare bar lengths.

A chart you'll show someone needs a title and a y label. Without them, "3.3" could be stars, a count or a percentage.

DTwo lines on one chart

SyntaxDoes
monthly.plot(label="monthly")
smooth.plot(label="2-month average")
Two Series plotted in the same Run share one chart: each .plot() on a Series draws onto the current chart. label= names each line.
plt.legend()Adds the key that says which line is which, using the labels.
ax = monthly.plot()
smooth.plot(ax=ax)
The explicit way: keep the Axes that the first .plot() returned, and pass it to the second, so it can't land on a new chart.

This is the Lesson 04 rolling average, now visible. The raw line jumps around because each month has only one to three reviews. The smoothed line shows the direction underneath, and you can see it lags a month behind.

📖Read the docs: the .plot() parametersRequired

Part 1 showed four keyword arguments. .plot() takes about thirty, and nobody memorises them: you look them up. Finding one you weren't taught is the skill here. At work, the docs are where you check what a method really does in your pandas version.

  1. Open Series.plot.bar (pandas 3.0, the version this page runs). Read its Parameters: the last one, **kwargs, says where the other keyword arguments are documented. Follow that link.
  2. On that page, read down Parameters and find the one that turns the tick labels (the product names under the bars). Note its name and what kind of value it takes.
  3. Glance at Returns: it's the same Axes as section A.

Docs · Names you can read

Run the starter: pandas turns bar labels sideways by default. Using the parameter you found, redraw the chart with the product names level (not rotated at all).

Hint

The answer isn't in this lesson. On the DataFrame.plot page, scan the Parameters list for the one that sets the angle of the tick labels, and check what value means level. It's one more keyword argument inside .plot.bar(…).

Part 2 · Practice

Now draw it yourself

Each run starts from the original table, where date is text, and with a blank chart. The checks read your chart: what kind it is, its values and its labels.

1 · The monthly trend

Set monthly to the average stars per month, then draw it as a line chart.

Hint

First the monthly Series, as in Lesson 04. Then one method on it draws the line. See A.

2 · Compare the products

Draw a bar chart of the average stars per product, highest first.

Hint

groupby → mean → sort_values(ascending=False), then the bar method. See B.

3 · Reviews per month

Draw a bar chart of how many reviews each month got, with the months in time order.

Hint

Count each month with value_counts() on df["date"].dt.to_period("M"). It sorts by count, so which method puts the labels back in order? Then B.

4 · Make it presentable

Redraw the product chart with a title (any wording), a y-axis label, and a y-axis that runs from 0 to 5.

Hint

Three keyword arguments inside .plot.bar(…), or three plt. lines after it. See C.

Stretch · Raw and smoothed together

On one chart, draw monthly and its 2-month rolling average as two lines, with a legend.

Hint

smooth is .rolling(2).mean() from Lesson 04. Then two .plot(label=…) lines and the legend. See D.

Part 3 · Remember

Spaced review

Next, do the practice set until this lesson's skills are mastered (3 clean solves each). Then its 4 cards join your Daily review from the next day.

Use it at work tomorrow

Turn last lesson's numbers into a picture you can paste into a message or a slide. On your review pipeline's output:

  1. Draw one product's average rating per month as a line, with ylim=(1, 5) and a title.
  2. If months have few reviews, add its .rolling(3).mean() as a second line, with a legend.
  3. Draw a bar chart of average rating per product, highest first, starting at 0.
  4. Save it with plt.savefig("ratings.png", bbox_inches="tight") on the line after the chart. The file appears next to your notebook.

Before you share a chart, read it out loud: "average stars per month, for the AC, January to June". If you can't say what the axes are, add the labels.

Primary source

Python for Data Analysis ch. 9: Plotting and Visualization, by Wes McKinney. §9.2 "Line Plots" and "Bar Plots" cover .plot(), .plot.bar() and .plot.barh(). §9.1 "Ticks, Labels, and Legends" and "Saving Plots to File" cover titles, legends and savefig.

Ask me anything. Charts can go wrong in many small ways: squashed labels, months in the wrong order, two charts where you wanted one. If yours looks odd, paste the code and describe what you see.