Bird
0
0

You want to show custom hover labels on a scatter plot with points labeled by their index. Which approach using mplcursors is correct?

hard📝 Application Q8 of 15
Matplotlib - Interactive Features
You want to show custom hover labels on a scatter plot with points labeled by their index. Which approach using mplcursors is correct?
ASet labels using <code>plt.annotate()</code> before calling mplcursors
BUse <code>cursor.connect('add', lambda sel: sel.annotation.set_text(f'Index: {sel.index}'))</code>
CPass a list of labels directly to <code>mplcursors.cursor()</code> as an argument
DUse <code>cursor.set_labels()</code> method with the label list
Step-by-Step Solution
Solution:
  1. Step 1: Understand custom label setting in mplcursors

    Use the 'add' event to set annotation text dynamically via a lambda function.
  2. Step 2: Evaluate options

    mplcursors.cursor() does not accept label lists directly; plt.annotate() is static; set_labels() does not exist.
  3. Final Answer:

    Use cursor.connect('add', lambda sel: sel.annotation.set_text(f'Index: {sel.index}')) -> Option B
  4. Quick Check:

    Custom labels via event connection [OK]
Quick Trick: Use cursor.connect('add', func) to customize hover labels [OK]
Common Mistakes:
  • Trying to pass labels directly
  • Using plt.annotate() for dynamic hover labels

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes