Bird
0
0

Identify the error in this code that prevents hover labels from showing:

medium📝 Debug Q14 of 15
Matplotlib - Interactive Features
Identify the error in this code that prevents hover labels from showing:
import matplotlib.pyplot as plt
import mplcursors

fig, ax = plt.subplots()
line, = ax.plot([1, 2, 3], [4, 5, 6])
mplcursors.cursor(line)
plt.show()
AThe variable 'line' should be a list, not a single Line2D object
BThe plot command is missing marker style to show points
Cmplcursors is not imported correctly
Dmplcursors.cursor() must be called before plotting
Step-by-Step Solution
Solution:
  1. Step 1: Analyze plot and cursor usage

    The code plots a line without markers. Hover labels appear on points, but here points are not visible because no markers are set.
  2. Step 2: Identify why hover labels don't show

    mplcursors works on plotted points. Without markers, the line is continuous and no discrete points exist to hover on, so labels don't appear.
  3. Final Answer:

    The plot command is missing marker style to show points -> Option B
  4. Quick Check:

    Missing markers = no hover labels [OK]
Quick Trick: Add markers to plot for mplcursors hover labels [OK]
Common Mistakes:
  • Thinking mplcursors must be called before plot
  • Assuming single Line2D object is invalid input
  • Believing import error causes no labels

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes