Chapter 1 · Section 5 practice
Matplotlib
Work from question 1 to 10. Edit your Python box, click Run Code, review the result, then click Submit attempt. Reference answers unlock after all ten attempts are submitted. This records completion, not correctness. Each box runs independently. Progress is saved in this browser.
1. Complete a title
Warm-up. Run this chart after adding the title Daily customers and a show call. Print “Line chart displayed”.
Write your answer, then run it.
2. Label units
Warm-up. Make a line chart for days [1, 2, 3] and revenue [3200, 3500, 3300]. Label axes Day and Revenue (HK$/day). Print “Units labelled”.
Write your answer, then run it.
3. Compare categories
Warm-up. Draw bars for Tea and Coffee with counts [30, 20]. Add labels and print the larger category.
Write your answer, then run it.
4. Show delivery spread
Build your skills. Draw a histogram of the supplied times using four bins. Label both axes and print the number of observations.
Write your answer, then run it.
5. Inspect an association
Build your skills. Draw a scatter plot of spend [100, 200, 300] and orders [80, 100, 110]. Add a comment explaining why the plot alone cannot prove causation. Print “Association is not causation”.
Write your answer, then run it.
6. Plot a labelled Series
Build your skills. Use a Series method to draw a bar chart of the supplied dish revenues. Keep labels horizontal and print the largest dish.
Write your answer, then run it.
7. Change histogram detail
Build your skills. Draw the delivery histogram with three bins, then six bins, in separate figures. Label both charts and print “Same data, different bins”.
Write your answer, then run it.
8. Choose a suitable chart
Challenge. For unordered dishes [Wonton, Char Siu, Milk Tea], select a chart to compare revenues [3570, 6380, 3920]. Draw it, then print your chart type and explain the choice in a comment.
Write your answer, then run it.
9. Plot a calculated quantity
Challenge. Compute daily revenues from counts [80, 90, 100] and a fixed HK$40 bill. Plot against Mon, Tue, Wed. Print the total and explain the trend limitation.
Write your answer, then run it.
10. Revenue is not profit
Challenge. Plot dish revenues [3570, 6380, 3920] as bars with a zero baseline. Print the highest-revenue dish and add two comments: one supported conclusion and one claim the data cannot support.
Write your answer, then run it.
Reference answers are locked until all 10 attempts are submitted.
1. Complete a title
import matplotlib.pyplot as plt
plt.figure(figsize=(6, 3))
plt.plot([1, 2, 3], [100, 120, 110])
plt.title("Daily customers")
plt.xlabel("Day")
plt.ylabel("Customers")
plt.tight_layout()
plt.show()
print("Line chart displayed")Expected output
Line chart displayedThe output also contains a line chart with its title and labels.
2. Label units
import matplotlib.pyplot as plt
plt.figure(figsize=(6, 3))
plt.plot([1, 2, 3], [3200, 3500, 3300])
plt.title("Daily revenue")
plt.xlabel("Day")
plt.ylabel("Revenue (HK$/day)")
plt.tight_layout()
plt.show()
print("Units labelled")Expected output
Units labelledLabels make the values interpretable; the chart also appears.
3. Compare categories
import matplotlib.pyplot as plt
plt.figure(figsize=(6, 3))
plt.bar(["Tea", "Coffee"], [30, 20])
plt.title("Orders by drink")
plt.xlabel("Drink")
plt.ylabel("Number of orders")
plt.tight_layout()
plt.show()
print("Tea")Expected output
TeaThe chart has two bars. Tea has the larger observed count.
4. Show delivery spread
import matplotlib.pyplot as plt
times = [20, 25, 28, 30, 35, 40, 42, 50]
plt.figure(figsize=(6, 3))
plt.hist(times, bins=4, edgecolor="white")
plt.title("Delivery-time distribution")
plt.xlabel("Time (minutes)")
plt.ylabel("Number of deliveries")
plt.tight_layout()
plt.show()
print(len(times))Expected output
8The histogram contains eight observations grouped into four bins.
5. Inspect an association
import matplotlib.pyplot as plt
plt.figure(figsize=(6, 3))
plt.scatter([100, 200, 300], [80, 100, 110])
plt.title("Spend and orders")
plt.xlabel("Spend (HK$/day)")
plt.ylabel("Orders/day")
plt.tight_layout()
plt.show()
print("Association is not causation")
# Other factors may affect both variables.Expected output
Association is not causationThe plot shows three observations, not an identified causal effect.
6. Plot a labelled Series
import pandas as pd
import matplotlib.pyplot as plt
revenue = pd.Series([3570, 6380, 3920], index=["Wonton", "Char Siu", "Milk Tea"])
revenue.plot(kind="bar", figsize=(6, 3), rot=0)
plt.title("Revenue by dish")
plt.xlabel("Dish")
plt.ylabel("Revenue (HK$/day)")
plt.tight_layout()
plt.show()
print(revenue.idxmax())Expected output
Char SiuThe Series index supplies category labels. Char Siu has the highest revenue.
7. Change histogram detail
import matplotlib.pyplot as plt
times = [20, 25, 28, 30, 35, 40, 42, 50]
for bins in [3, 6]:
plt.figure(figsize=(6, 3))
plt.hist(times, bins=bins, edgecolor="white")
plt.title(f"Delivery times: {bins} bins")
plt.xlabel("Time (minutes)")
plt.ylabel("Number of deliveries")
plt.tight_layout()
plt.show()
print("Same data, different bins")Expected output
Same data, different binsBoth charts use the same observations; binning changes the visual detail.
8. Choose a suitable chart
import matplotlib.pyplot as plt
plt.figure(figsize=(6, 3))
plt.bar(["Wonton", "Char Siu", "Milk Tea"], [3570, 6380, 3920])
plt.title("Revenue by dish")
plt.xlabel("Dish")
plt.ylabel("Revenue (HK$/day)")
plt.tight_layout()
plt.show()
print("Bar")
# Unordered categories are compared by bar heights.Expected output
BarConnecting categories with a line can imply an order that is not part of the question.
9. Plot a calculated quantity
import numpy as np
import matplotlib.pyplot as plt
revenue = np.array([80, 90, 100]) * 40
plt.figure(figsize=(6, 3))
plt.plot(["Mon", "Tue", "Wed"], revenue, marker="o")
plt.title("Daily revenue")
plt.xlabel("Day")
plt.ylabel("Revenue (HK$/day)")
plt.tight_layout()
plt.show()
print(revenue.sum())
# Three increasing observations do not establish a lasting trend.Expected output
10800The calculated total is HK$10,800; the chart contains only three days.
10. Revenue is not profit
import pandas as pd
import matplotlib.pyplot as plt
revenue = pd.Series([3570, 6380, 3920], index=["Wonton", "Char Siu", "Milk Tea"])
revenue.plot(kind="bar", figsize=(6, 3), rot=0)
plt.ylim(bottom=0)
plt.title("Daily dish revenue")
plt.xlabel("Dish")
plt.ylabel("Revenue (HK$/day)")
plt.tight_layout()
plt.show()
print(revenue.idxmax())
# Supported: Char Siu has the largest observed revenue.
# Unsupported: Char Siu has the largest profit; costs are absent.Expected output
Char SiuBars support a revenue comparison, not a profit or causal claim.