Challenge - 5 Problems
Mplcursors Hover Label Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of mplcursors hover label code
What will be the output when running this code snippet that uses mplcursors to add hover labels to a scatter plot?
Matplotlib
import matplotlib.pyplot as plt import mplcursors fig, ax = plt.subplots() sc = ax.scatter([1, 2, 3], [4, 5, 6]) cursor = mplcursors.cursor(sc) cursor.connect("add", lambda sel: sel.annotation.set_text(f"Point: {sel.target[0]}, {sel.target[1]}")) plt.show()
Attempts:
2 left
💡 Hint
Check how mplcursors connects to the scatter plot and sets annotation text.
✗ Incorrect
The code creates a scatter plot and uses mplcursors to add interactive hover labels showing the coordinates of each point.
🧠 Conceptual
intermediate1:30remaining
Understanding mplcursors cursor targets
In mplcursors, what does the 'sel.target' attribute represent when handling the 'add' event for a scatter plot?
Attempts:
2 left
💡 Hint
Think about what information you want to show in a hover label.
✗ Incorrect
'sel.target' holds the data coordinates (x, y) of the point that the cursor is currently over.
🔧 Debug
advanced2:30remaining
Why does this mplcursors hover label code fail?
Given this code, why does it fail to show hover labels?
import matplotlib.pyplot as plt
import mplcursors
fig, ax = plt.subplots()
sc = ax.scatter([1, 2, 3], [4, 5, 6])
cursor = mplcursors.cursor(sc)
cursor.connect("add", lambda sel: sel.annotation.set_text(f"Point: {sel.target}"))
plt.show()
Attempts:
2 left
💡 Hint
Check what 'sel.target' contains and how it is formatted in the label.
✗ Incorrect
The 'sel.target' is a numpy array of coordinates. Printing it directly in an f-string shows array syntax, which is not user-friendly. It should be formatted to show individual coordinates.
❓ data_output
advanced1:30remaining
Number of annotations created by mplcursors
If you run mplcursors on a scatter plot with 5 points and hover over 3 different points sequentially, how many annotation objects will mplcursors create?
Attempts:
2 left
💡 Hint
Think about how mplcursors creates annotations on demand when points are hovered.
✗ Incorrect
Mplcursors creates a new annotation each time a new point is hovered over and the 'add' event triggers. Hovering 3 different points creates 3 annotations.
🚀 Application
expert3:00remaining
Customizing mplcursors hover labels with additional data
You have a scatter plot with points and a list of labels: labels = ['A', 'B', 'C']. How do you modify mplcursors to show the label along with coordinates on hover?
Matplotlib
import matplotlib.pyplot as plt import mplcursors x = [1, 2, 3] y = [4, 5, 6] labels = ['A', 'B', 'C'] fig, ax = plt.subplots() sc = ax.scatter(x, y) cursor = mplcursors.cursor(sc) # Fill in the lambda function below to show label and coordinates cursor.connect("add", lambda sel: sel.annotation.set_text(???)) plt.show()
Attempts:
2 left
💡 Hint
Use sel.index to get the index of the hovered point to access the label list.
✗ Incorrect
The 'sel.index' attribute gives the index of the hovered point, which can be used to get the corresponding label from the labels list.