0
0
Matplotlibdata~20 mins

Mplcursors for hover labels in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mplcursors Hover Label Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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()
AThe code raises a NameError because mplcursors is not imported.
BA scatter plot appears. Hovering over points shows labels like 'Point: 1, 4', 'Point: 2, 5', etc.
CThe scatter plot appears but no hover labels show when hovering over points.
DThe code raises a TypeError due to incorrect lambda function syntax.
Attempts:
2 left
💡 Hint
Check how mplcursors connects to the scatter plot and sets annotation text.
🧠 Conceptual
intermediate
1:30remaining
Understanding mplcursors cursor targets
In mplcursors, what does the 'sel.target' attribute represent when handling the 'add' event for a scatter plot?
AThe x and y coordinates of the data point being hovered over.
BThe color and size of the scatter plot markers.
CThe matplotlib figure object containing the plot.
DThe index of the data point in the original dataset.
Attempts:
2 left
💡 Hint
Think about what information you want to show in a hover label.
🔧 Debug
advanced
2: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()
AThe f-string tries to print 'sel.target' directly, which is an array, causing the label to be unreadable.
BThe lambda function is missing a return statement, causing a runtime error.
CThe scatter plot is missing a label parameter, so mplcursors cannot create annotations.
DThe 'connect' method is called with the wrong event name; it should be 'hover' instead of 'add'.
Attempts:
2 left
💡 Hint
Check what 'sel.target' contains and how it is formatted in the label.
data_output
advanced
1: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?
A1 annotation reused for all points hovered.
B5 annotations, one for each point in the scatter plot.
C0 annotations, because mplcursors does not create annotations automatically.
D3 annotations, one for each hovered point.
Attempts:
2 left
💡 Hint
Think about how mplcursors creates annotations on demand when points are hovered.
🚀 Application
expert
3: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()
Alambda sel: sel.annotation.set_text(f"Label: {labels[sel.annotation]}, Point: {sel.target[0]}, {sel.target[1]}")
Blambda sel: sel.annotation.set_text(f"Label: {labels[sel.target]}, Point: {sel.target[0]}, {sel.target[1]}")
Clambda sel: sel.annotation.set_text(f"Label: {labels[sel.index]}, Point: {sel.target[0]}, {sel.target[1]}")
Dlambda sel: sel.annotation.set_text(f"Label: {labels[sel]}, Point: {sel.target[0]}, {sel.target[1]}")
Attempts:
2 left
💡 Hint
Use sel.index to get the index of the hovered point to access the label list.