Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the mplcursors library.
Matplotlib
import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing matplotlib instead of mplcursors.
Trying to import numpy or pandas which are unrelated here.
✗ Incorrect
mplcursors is the library used to add interactive hover labels in matplotlib plots.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using plot() which draws lines between points.
Using bar() or hist() which are for different chart types.
✗ Incorrect
Scatter plots are created with the scatter() method in matplotlib.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using hover() which does not exist in mplcursors.
Using connect() or select() which are not valid functions here.
✗ Incorrect
The correct function to create interactive cursors is mplcursors.cursor().
4fill in blank
hardFill 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'
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.
✗ Incorrect
The x and y coordinates are at index 0 and 1 in sel.target respectively.
5fill in blank
hardFill 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'
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.
✗ Incorrect
The dictionary keys are points (x), filtering points where y (index 1) is greater than 0.