0
0
Matplotlibdata~10 mins

Pick events for data interaction 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 connect the pick event handler to the figure.

Matplotlib
fig.canvas.mpl_connect('[1]', on_pick)
Drag options to blanks, or click blank then click option'
Aresize_event
Bpick_event
Ckey_press_event
Dbutton_press_event
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'button_press_event' instead of 'pick_event'.
Confusing key press events with pick events.
2fill in blank
medium

Complete the code to make the scatter plot points pickable.

Matplotlib
scatter = ax.scatter(x, y, picker=[1])
Drag options to blanks, or click blank then click option'
ATrue
BFalse
CNone
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Setting picker to False disables picking.
Using None does not enable picking.
3fill in blank
hard

Fix the error in the pick event handler to print the index of the picked point.

Matplotlib
def on_pick(event):
    ind = event.ind[[1]]
    print(f"Picked point index: {ind}")
Drag options to blanks, or click blank then click option'
A-1
B1
C0
Dlen(event.ind)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 causes index error if only one point is picked.
Using len(event.ind) is out of range.
4fill in blank
hard

Fill both blanks to create a pick event handler that changes the color of the picked point to red.

Matplotlib
def on_pick(event):
    ind = event.ind[0]
    scatter._facecolors[ind] = [1]
    scatter.figure.canvas.[2]()
Drag options to blanks, or click blank then click option'
A[1, 0, 0, 1]
B[0, 1, 0, 1]
Cdraw_idle
Dshow
Attempts:
3 left
💡 Hint
Common Mistakes
Using green color instead of red.
Calling show() instead of draw_idle() to refresh.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps point indices to their x-coordinates for points with x > 0.5.

Matplotlib
selected_points = [1]: x[[2]] for [1] in range(len(x)) if x[[2]] > 0.5}
Drag options to blanks, or click blank then click option'
Ai
Bx
Dj
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for keys and values causing errors.
Using 'x' as index variable instead of 'i'.