Look at the line plot with and without annotations. Which statement best describes how annotations help tell the data story?
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] plt.figure(figsize=(6,4)) plt.plot(x, y, marker='o') plt.title('Prime Numbers Line Plot') plt.xlabel('Index') plt.ylabel('Prime Number') # Annotation added plt.annotate('Highest value', xy=(5, 11), xytext=(3, 10), arrowprops=dict(facecolor='black', shrink=0.05)) plt.show()
Think about why someone would add text and arrows to a plot.
Annotations point out important parts of the data, helping viewers quickly see what matters most.
What will be the output of this code snippet?
import matplotlib.pyplot as plt x = [1, 2, 3, 4] y = [10, 20, 25, 30] plt.scatter(x, y) plt.annotate('Peak', xy=(4, 30), xytext=(3, 28), arrowprops=dict(facecolor='red')) plt.show()
Check how plt.annotate uses xy and xytext to place the arrow and text.
The code creates a scatter plot and adds an annotation with an arrow pointing exactly to the point (4, 30) labeled 'Peak'.
Why does this code raise an error?
import matplotlib.pyplot as plt x = [1, 2, 3] y = [5, 7, 9] plt.plot(x, y) plt.annotate('Max', xy=(3, 9), xytext=(4, 10), arrowprops=dict(facecolor='blue')) plt.show()
Check the parentheses carefully in the annotate line.
The annotate line is missing a closing parenthesis, causing a syntax error.
Given this code, how many annotations will appear on the plot?
import matplotlib.pyplot as plt x = [1, 2, 3, 4] y = [4, 3, 2, 1] plt.plot(x, y) for i, val in enumerate(y): if val < 3: plt.annotate(f'Low {val}', xy=(x[i], val), xytext=(x[i], val+0.5), arrowprops=dict(facecolor='green')) plt.show()
Look at the condition val < 3 and count how many y values satisfy it.
Only y values 2 and 1 are less than 3, so 2 annotations are added.
Which of the following best explains why annotations are important in telling a data story?
Think about how annotations help a viewer understand the story behind the numbers.
Annotations add explanations and focus attention on important parts, making the data story clearer.