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.
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.
| product | date | stars | |
|---|---|---|---|
| 0 | Purifier | 2026-01-06 | 5 |
| 1 | Heater | 2026-01-19 | 4 |
| 2 | Dryer | 2026-02-02 | 4 |
| 3 | Purifier | 2026-02-14 | 4 |
| 4 | Heater | 2026-02-27 | 5 |
| 5 | Purifier | 2026-03-05 | 2 |
| 6 | Dryer | 2026-03-21 | 3 |
| 7 | Purifier | 2026-04-08 | 2 |
| 8 | Heater | 2026-04-25 | 4 |
| 9 | Dryer | 2026-05-11 | 3 |
| 10 | Purifier | 2026-05-30 | 1 |
| 11 | Heater | 2026-06-16 | 5 |
Part 1 · Learn
The syntax, and what each piece draws
AA line chart of a trend
| Syntax | Draws |
|---|---|
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 returns | An 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
| Syntax | Draws |
|---|---|
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
| Syntax | Does |
|---|---|
.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
| Syntax | Does |
|---|---|
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.
- 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. - 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.
- Glance at Returns: it's the same
Axesas 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:
- Draw one product's average rating per month as a line, with
ylim=(1, 5)and a title. - If months have few reviews, add its
.rolling(3).mean()as a second line, with a legend. - Draw a bar chart of average rating per product, highest first, starting at 0.
- 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.
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.