PY-401 ยท Visualisation
Choosing the right chart
โฑ 9 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
Pick the chart from the question: a line chart for change over time, a bar chart to compare groups, a histogram to see how values are spread, and a scatter plot to see if two numbers move together. Avoid pie charts with more than a few slices, because people compare lengths far better than angles. A good chart answers one question at a glance.
- Match a question to a chart type
- Draw a histogram and a scatter plot
- Avoid charts that mislead
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
import matplotlib.pyplot as pltimport pandas as pdshops = pd.DataFrame({"ad_spend": [5, 8, 12, 3, 15, 10, 7, 20],"sales": [52, 61, 80, 40, 95, 70, 58, 118],"delivery_days": [2, 3, 2, 5, 1, 2, 3, 2],})fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 3))ax1.hist(shops["delivery_days"], bins=5)ax1.set_title("Most deliveries take 2 days")ax2.scatter(shops["ad_spend"], shops["sales"])ax2.set_title("More ad spend, more sales")print("Correlation:", round(shops["ad_spend"].corr(shops["sales"]), 2))
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
Correlation: 1.0 ๐ฏ Draw a scatter plot of hours (x) against score (y) on ax, and set r to their correlation rounded to 2 decimal places.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
ax.scatter(study["hours"], study["score"]); r = round(study["hours"].corr(study["score"]), 2)
- The problemTwo analysts show the same data. One uses a 12-slice pie chart and the room squints. The other uses sorted bars and the decision is made in a minute.
- The Python idea
ax.hist(values, bins=10)Pick the chart from the question: a line chart for change over time, a bar chart to compare groups, a histogram to see how values are spread, and a scatter plot to see if two numbers move together. - In real lifeA food delivery app plots a histogram of delivery times per city. Two cities with the same average look different: one has a long tail of 90-minute orders that the average hid.
Start with a question
Two analysts show the same data. One uses a 12-slice pie chart and the room squints. The other uses sorted bars and the decision is made in a minute.
The wrong chart hides the answer or, worse, suggests a wrong one. Choosing well is what makes analysis useful to other people.
Explain it like I'm 10
Charts are tools in a toolbox. A line is a ruler for time, bars are for "who has more", a histogram shows "how many are small, medium or big", and dots show "does one go up when the other does?"
The technical explanation
Line: one value over time, such as daily sales. Bar: compare categories, such as revenue per region.
Histogram (ax.hist(values, bins=10)): how values are spread, such as delivery times. It shows the typical value and the outliers.
Scatter (ax.scatter(x, y)): whether two numbers are related, such as ad spend and sales. df["a"].corr(df["b"]) puts a number on it, from -1 to 1.
Several charts in one figure: fig, (ax1, ax2) = plt.subplots(1, 2) gives two axes side by side.
| Your question | Chart |
|---|---|
How did it change over time? | Line |
Which group is biggest? | Bar (sorted) |
How are the values spread? | Histogram |
Do two numbers move together? | Scatter |
What share is each part (2โ4 parts)? | Pie or stacked bar |
Syntax
ax.hist(values, bins=10)
ax.scatter(x, y)
fig, (ax1, ax2) = plt.subplots(1, 2)
df["a"].corr(df["b"]) Real-world scenario
A food delivery app plots a histogram of delivery times per city. Two cities with the same average look different: one has a long tail of 90-minute orders that the average hid.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Exploratory analysis checklist โQuestions people ask
When should I use a histogram instead of a bar chart?
Use a histogram for one numeric column to see how its values are spread across ranges. Use a bar chart to compare a value between named categories such as regions or products.
Does a high correlation mean one thing causes the other?
No. Correlation only says two numbers move together. A third factor may drive both, so test causes with experiments or domain knowledge.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner When should I use a histogram instead of a bar chart?
Use a histogram for one numeric column to see how its values are spread across ranges. Use a bar chart to compare a value between named categories such as regions or products.
beginner Does a high correlation mean one thing causes the other?
No. Correlation only says two numbers move together. A third factor may drive both, so test causes with experiments or domain knowledge.
intermediate Best chart for daily visitors over a month?
Line. Lines show change over time.
intermediate Best chart to compare revenue across 6 regions?
Sorted bar. Bars compare categories; sorting helps.
Quick revision
Remember
- Match a question to a chart type
- Draw a histogram and a scatter plot
- Avoid charts that mislead
- Bar charts must start at zero. Cutting the axis at 90 makes a 2% difference look like a doubling.
Syntax
ax.hist(values, bins=10)
ax.scatter(x, y)
fig, (ax1, ax2) = plt.subplots(1, 2)
df["a"].corr(df["b"]) Official documentation
- matplotlib plot types (docs.python.org)