0
0
Matplotlibdata~10 mins

Mplcursors for hover labels in Matplotlib - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the mplcursors library.

Matplotlib
import [1]
Drag options to blanks, or click blank then click option'
Amplcursors
Bmatplotlib
Cnumpy
Dpandas
Attempts:
3 left
💡 Hint
Common Mistakes
Importing matplotlib instead of mplcursors.
Trying to import numpy or pandas which are unrelated here.
2fill in blank
medium

Complete the code to create a scatter plot with matplotlib.

Matplotlib
fig, ax = plt.subplots()
ax.[1](x, y)
Drag options to blanks, or click blank then click option'
Ascatter
Bplot
Cbar
Dhist
Attempts:
3 left
💡 Hint
Common Mistakes
Using plot() which draws lines between points.
Using bar() or hist() which are for different chart types.
3fill in blank
hard

Fix the error in the code to enable hover labels using mplcursors.

Matplotlib
cursor = mplcursors.[1](ax.collections)
Drag options to blanks, or click blank then click option'
Aselect
Bconnect
Chover
Dcursor
Attempts:
3 left
💡 Hint
Common Mistakes
Using hover() which does not exist in mplcursors.
Using connect() or select() which are not valid functions here.
4fill in blank
hard

Fill both blanks to add a custom label format for the hover labels.

Matplotlib
cursor.connect("add", lambda sel: sel.annotation.set_text(f"X: {sel.target[[1]]:.2f}\nY: {sel.target[[2]]:.2f}"))
Drag options to blanks, or click blank then click option'
A0
B1
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using indices 1 and 2 which are out of range for 2D points.
Using 2 or 3 which do not exist in the coordinate tuple.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps points to their labels with conditions.

Matplotlib
labels = { [1]: f"Point {x}" for x in points if x[[2]] [3] 0 }
Drag options to blanks, or click blank then click option'
Ax
B1
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 0 instead of 1 for y coordinate.
Using '<' instead of '>' for the condition.
Using something other than the point as the key.