Choosing a Chart
Overview
The shape of the question decides the shape of the picture.
What You Will Learn in This Lesson
By the end of this lesson, you will be able to:
- Match four common question shapes to four chart types.
- Say what the table behind each chart looks like.
- Explain why a pie chart is usually the wrong answer.
- Build the summary table before thinking about the picture.
Why This Matters
Choosing the chart is not a decoration step — it is the last analysis decision. A bar chart and a line chart of the same numbers make different claims, and picking the one that matches your question is what stops the picture saying something the data does not.
Step 1: Four questions, four charts
Almost every chart you need is one of these, and the question tells you which.
The mapping
Comparison across categories is a bar. Change over time is a line. The distribution of one column is a histogram. The relationship between two numbers is a scatter.
# Comparison -> bar mean score per team
# Over time -> line sales per month
# Distribution-> histogram how scores are spread
# Relationship-> scatter hours against score
print("the question decides, not the tool")
Why bars work
Bars share a baseline and encode value as length, which is the comparison people read most accurately. A pie encodes it as angle, which they read worst — and it can only ever show parts of one whole.
import pandas as pd
df = pd.DataFrame({"team": ["red", "red", "blue"], "score": [90, 80, 70]})
print(df.groupby("team")["score"].mean().round(1))
Mini Practice #1: Name the chart
Try It YourselfFor each question, write down which of the four charts it asks for before reading on.
Step 2: What the code looks like
The chart is one call. The table behind it is the part worth getting right, and the part you can check.
Table first
Every chart in this lesson is a summary table plus a .plot. If the table is wrong, the chart is wrong and much more convincing.
import pandas as pd
df = pd.DataFrame({"team": ["red", "red", "blue"], "score": [90, 80, 70]})
summary = df.groupby("team")["score"].mean().round(1)
print(summary)
# summary.plot(kind="bar") # the picture, once the table is right
Sort a bar chart by value
Alphabetical order carries no information. Ordering the bars by the thing being compared is what makes the ranking readable at a glance.
import pandas as pd
df = pd.DataFrame({"team": ["zeta", "alpha", "mu"], "score": [70.0, 90.0, 80.0]})
means = df.groupby("team")["score"].mean()
print(means)
print(means.sort_values(ascending=False))
The exception is a category with a natural order — months, age bands, school years. Sorting those by value destroys the thing the reader is using to navigate.
Mini Practice #2: Order the bars
Try It YourselfProduce the mean per team and then the same table ordered so the ranking is readable.
Step 3: When to Use It
Comparing categories
A bar chart of one summary per group, sorted by value.
df.groupby("team")["score"].mean().sort_values()
Showing change over time
A line chart of a series with dates on the index.
When Not to Use It
Do not use a pie chart to compare more than two or three parts, or a line chart to connect categories that have no order.
End-of-Lesson Exercises
Exercise 1: The table behind a bar chart
Write chart_data(df) that returns the table behind a bar chart of mean score per team, as a dictionary of team to mean rounded to one decimal place.
A bar chart of "mean by team" is a group-by with the means rounded for display. Get that table right and the chart is one more line.
Exercise 2: Bars in a readable order
Write ranked_bars(df) returning a list of (team, mean) pairs ordered highest mean first, with the means to one decimal place. Alphabetical order carries no information.
Group to a mean per team, round, sort_values(ascending=False), then build plain (name, value) tuples with str() and float().