B. The parameter 'point' is invalid; should use 'xy' instead
C. plt.plot needs labels before annotation
D. The text 'Max' should be a number
Solution
Step 1: Check plt.annotate parameters
The correct parameter for the point to annotate is 'xy', not 'point'.
Step 2: Identify the error cause
Using 'point' causes a TypeError because plt.annotate does not recognize it.
Final Answer:
The parameter 'point' is invalid; should use 'xy' instead -> Option B
Quick Check:
Use xy= for annotation point [OK]
Hint: Use xy=, not point=, to specify annotation location [OK]
Common Mistakes:
Using wrong parameter names like point or position
Placing plt.show() before annotation
Thinking annotation text must be numeric
5. You want to highlight the maximum value point on a plot with an annotation that places the label 10 units above the point and draws an arrow. Which code correctly achieves this for data x=[1,2,3], y=[4,9,7]?
hard
A. plt.plot(x, y)\nmax_idx = y.index(max(y))\nplt.annotate('Max', xy=(x[max_idx], y[max_idx]), xytext=(x[max_idx], y[max_idx]+10), arrowprops=dict(arrowstyle='->'))\nplt.show()
B. plt.plot(x, y)\nmax_idx = y.index(max(y))\nplt.annotate('Max', xytext=(x[max_idx], y[max_idx]), xy=(x[max_idx], y[max_idx]+10), arrowprops=dict(arrowstyle='->'))\nplt.show()
C. plt.plot(x, y)\nmax_val = max(y)\nplt.annotate('Max', xy=(max_val, x[max_idx]), xytext=(max_val, x[max_idx]+10), arrowprops=dict(arrowstyle='->'))\nplt.show()
D. plt.plot(x, y)\nmax_idx = y.index(max(y))\nplt.annotate('Max', xy=(x[max_idx], y[max_idx]), xytext=(x[max_idx], y[max_idx]-10), arrowprops=dict(arrowstyle='->'))\nplt.show()
Solution
Step 1: Find the max value index correctly
max_idx = y.index(max(y)) finds the index of the highest y value.
Step 2: Set annotation point and label position
xy is the max point (x[max_idx], y[max_idx]). xytext places label 10 units above: y[max_idx]+10.
Step 3: Use arrowprops to draw arrow
arrowprops=dict(arrowstyle='->') correctly adds an arrow from label to point.
Final Answer:
Code in option A correctly annotates max point with label 10 units above and arrow -> Option A
Quick Check:
xy=point, xytext=above point, arrowprops=arrow [OK]
Hint: Use xytext=(x, y+offset) to place label above point [OK]
Common Mistakes:
Swapping xy and xytext parameters
Using wrong coordinates for annotation
Placing label below instead of above
Mixing x and y values in annotation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.