A. arrowprops should be a dict, but it is given as a list
B. The code is correct and will run without errors
C. Missing import statement for matplotlib.pyplot
D. xy and xytext coordinates are reversed
Solution
Step 1: Check code completeness
The snippet uses plt but does not import matplotlib.pyplot as plt, which causes a NameError.
Step 2: Verify arrowprops and coordinates
arrowprops is correctly a dict, and coordinates are in correct order (xy=point, xytext=text).
Final Answer:
Missing import statement for matplotlib.pyplot -> Option C
Quick Check:
Import missing = error [OK]
Hint: Always import matplotlib.pyplot as plt before plotting [OK]
Common Mistakes:
Assuming arrowprops must be a list
Swapping xy and xytext
Ignoring missing imports
5. You want to annotate multiple points on a scatter plot with arrows pointing from text labels offset by (0.5, 0.5). Which code snippet correctly applies this for points in points = [(1,2), (3,4), (5,6)]?
hard
A. for x, y in points:
plt.annotate(f'({x},{y})', xy=(x, y), xytext=(x+0.5, y+0.5), arrowprops=dict(arrowstyle='->'))
B. for x, y in points:
plt.annotate(f'({x},{y})', xytext=(x, y), xy=(x+0.5, y+0.5), arrowprops=dict(arrowstyle='->'))
C. for x, y in points:
plt.annotate(f'({x},{y})', xy=(x+0.5, y+0.5), xytext=(x, y), arrowprops=dict(arrowstyle='->'))
D. for x, y in points:
plt.annotate(f'({x},{y})', xytext=(x+0.5, y+0.5), xy=(x, y))
Solution
Step 1: Understand annotation parameters for multiple points
Text should be offset by (0.5, 0.5) from each point, so xytext=(x+0.5, y+0.5) and arrow points to xy=(x, y).
Step 2: Check arrowprops and loop structure
Arrow style '->' is correct, and loop correctly iterates over points unpacking x and y.
Final Answer:
for x, y in points:
plt.annotate(f'({x},{y})', xy=(x, y), xytext=(x+0.5, y+0.5), arrowprops=dict(arrowstyle='->')) -> Option A
Quick Check:
xy = point, xytext = offset text [OK]
Hint: xy is point, xytext is offset text location [OK]
Common Mistakes:
Swapping xy and xytext causing arrows to point wrong way
Omitting arrowprops so no arrow appears
Not unpacking points correctly in loop
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.