Lesson 21 of 23
โฎPrev Nextโฏ

PY-401 ยท Visualisation

Choosing the right chart

โฑ 9 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

21of 23

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก 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.

    Choosing the right chart, running
    1. import matplotlib.pyplot as plt
    2. import pandas as pd
    3. shops = pd.DataFrame({
    4. "ad_spend": [5, 8, 12, 3, 15, 10, 7, 20],
    5. "sales": [52, 61, 80, 40, 95, 70, 58, 118],
    6. "delivery_days": [2, 3, 2, 5, 1, 2, 3, 2],
    7. })
    8. fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 3))
    9. ax1.hist(shops["delivery_days"], bins=5)
    10. ax1.set_title("Most deliveries take 2 days")
    11. ax2.scatter(shops["ad_spend"], shops["sales"])
    12. ax2.set_title("More ad spend, more sales")
    13. print("Correlation:", round(shops["ad_spend"].corr(shops["sales"]), 2))
    Press โ–ถ to watch Python run this, one line at a time.
    1. 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.
    2. The Python ideaax.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.
    3. 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 questionChart
    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"])

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    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.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. Best chart for daily visitors over a month?
    2. 2. Best chart to compare revenue across 6 regions?
    3. 3. What does a histogram show?
    4. 4. A correlation of 0.95 meansโ€ฆ
    5. 5. Why should a bar chart start at zero?

    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