Charts That Tell the Truth
Turn a pandas summary into a matplotlib chart, and learn the axis and scale choices that separate an honest chart from a misleading one.
A Number Nobody Reads Twice
Module 4 ended with a print statement: “Cold Brew leads all items with $60.00 in revenue.” True, clear, and still easy to skim past in a wall of terminal output. A chart is what makes that finding land — it’s the difference between a fact someone has to parse and a fact someone just sees. This lesson covers matplotlib, Python’s foundational charting library, and — just as important — the handful of choices that turn an accurate chart into a misleading one, because a chart’s persuasive power cuts both ways.
If matplotlib isn’t installed:
pip install matplotlib
We’re finishing with the same corner_cafe_sales.csv and the by_item revenue summary from Module 4.
Your First Chart
import pandas as pd
import matplotlib.pyplot as plt
sales = pd.read_csv("corner_cafe_sales.csv")
sales["revenue"] = sales["quantity"] * sales["price"]
by_item = sales.groupby("item")["revenue"].sum().sort_values(ascending=False)
plt.bar(by_item.index, by_item.values, color="#16a34a")
plt.title("Revenue by Item")
plt.xlabel("Item")
plt.ylabel("Revenue ($)")
plt.savefig("revenue_by_item.png")
plt.show()
plt.bar(x, y) takes two matching sequences — here, by_item.index (the item names) for the bar positions and by_item.values (the revenue totals) for their heights. plt.title, plt.xlabel, and plt.ylabel aren’t decoration — a chart with no labels forces every viewer to guess what they’re looking at, which defeats the entire point of charting a finding instead of just stating it. plt.savefig("revenue_by_item.png") writes the chart to a file, so it can go in a report or a slide; plt.show() opens it in a window if you’re running the script somewhere with a display. Call savefig before show() — once a chart window closes, matplotlib clears the figure, and a savefig after that point writes a blank image.
A line chart uses the same pattern for data that moves across an ordered sequence, like revenue over the three days:
by_day = sales.groupby("date")["revenue"].sum()
plt.plot(by_day.index, by_day.values, marker="o", color="#2563eb")
plt.title("Daily Revenue Trend")
plt.xlabel("Date")
plt.ylabel("Revenue ($)")
plt.savefig("daily_revenue_trend.png")
plt.show()
marker="o" draws a dot at each actual data point on top of the connecting line, which matters for a three-point line like this one — without it, three dots and the two segments between them can be hard to distinguish from a chart that has many more underlying points.
Choosing the Right Chart Shape
| Question | Chart | Why |
|---|---|---|
| Compare totals across categories | plt.bar | Bar length is easy to compare at a glance |
| Show a trend across an ordered sequence (time) | plt.plot | A line makes direction and rate of change visible |
| Show the relationship between two numeric variables | plt.scatter | Reveals whether two measures move together |
plt.scatter(x, y) follows the identical pattern — for example, plt.scatter(sales["quantity"], sales["revenue"]) would show whether bigger orders tend to be more profitable per order, one dot per row. The chart type isn’t a style preference; it’s a claim about what kind of comparison you’re asking the viewer to make, so pick the one that matches your actual question.
Where Charts Start Lying
A chart’s whole value is that people trust their eyes more than a table of numbers. That trust is exactly what a poorly-made chart abuses — sometimes by accident, sometimes on purpose. The single most common way a bar chart misleads: starting the y-axis somewhere other than zero.
# Distorts the comparison — don't do this
plt.bar(by_item.index, by_item.values, color="#16a34a")
plt.ylim(35, 62)
plt.ylim(35, 62) forces the visible range to start near the smallest bar instead of at zero. Because a bar’s height is what a viewer reads as “how much,” truncating the axis inflates every visual difference — a value that’s genuinely about 1.5 times larger than another can be drawn ten times taller once the baseline is cut away.
The fix is almost always to just not call plt.ylim at all — matplotlib defaults to a zero-based axis for a bar chart unless you override it, so leaving it alone is usually the honest choice by default. A truncated axis is occasionally defensible for a line chart zooming in on a small fluctuation in an otherwise large, stable number (a stock price, a temperature) — but even then, label the axis clearly and consider annotating that it’s zoomed, so no one mistakes a small wiggle for a dramatic swing.
Using corner_cafe_sales.csv:
import pandas as pd
import matplotlib.pyplot as plt
sales = pd.read_csv("corner_cafe_sales.csv")
sales["revenue"] = sales["quantity"] * sales["price"]
by_item = sales.groupby("item")["revenue"].sum().sort_values(ascending=False)
plt.bar(by_item.index, by_item.values, color="#16a34a")
plt.title("Corner Cafe: Revenue by Item")
plt.xlabel("Item")
plt.ylabel("Revenue ($)")
plt.savefig("revenue_by_item.png")
plt.show()Run it, then deliberately break it: add plt.ylim(35, 62) right before plt.savefig(...), regenerate the chart, and compare the two images side by side. Notice how much more dramatic Cold Brew’s lead looks in the truncated version — same four numbers, a visibly different (and less honest) impression.
Bar charts aren’t the only place scale choices mislead — pie charts have their own common failure, and it’s not about the axis.
# Technically works, but human eyes are bad at
# comparing angles — six-plus slices become an
# unreadable wedge of similar-looking colors
plt.pie(by_item.values, labels=by_item.index)A pie chart is only genuinely readable with a handful of slices and a clear largest-vs-rest story — beyond four or five categories, or when several slices are close in size, a bar chart communicates the same comparison far more clearly, because bar length is much easier for a human eye to judge accurately than a wedge angle. When in doubt, default to plt.bar for category comparisons; reach for plt.pie only when “this one slice dominates the rest” is genuinely the whole point.
A truncated y-axis on a bar chart is misleading. Would the same truncation be misleading on a scatter plot comparing quantity and revenue per order?
Reveal Answer & Explanation
Less so, and often not misleading at all — because a scatter plot’s meaning comes from each point’s position relative to the others, not from a bar’s height measured against a zero baseline.
Bar charts encode value as length from a shared baseline, so moving that baseline distorts every comparison the viewer makes by eye. A scatter plot encodes value as position — you’re reading where each dot sits relative to the other dots and the general trend, not measuring a bar’s length against zero. Zooming a scatter plot’s axes to the range where the actual data lives is normal practice and usually makes the pattern easier to see, not harder. The rule isn’t “never adjust an axis” — it’s “know what visual property your chart type uses to encode value, and don’t distort that specific property.” For bars, that’s height from zero. For scatter plots, it’s relative position, which a zoomed axis doesn’t corrupt the same way.
Up Next
You now have every piece: load data, clean it, filter and group it, state a finding, and chart it. The capstone lesson runs the entire pipeline start to finish on one new dataset, exactly the way a real data analysis unfolds — and it’s the last lesson in this course.