0
0
Matplotlibdata~20 mins

Pick events for data interaction in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pick Event Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when clicking on a scatter point?

Consider the following matplotlib code that plots points and prints the index of the clicked point.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
points = ax.scatter([1, 2, 3], [4, 5, 6], picker=True)

clicked_indices = []

def on_pick(event):
    ind = event.ind[0]
    clicked_indices.append(ind)
    print(f"Clicked point index: {ind}")

fig.canvas.mpl_connect('pick_event', on_pick)
plt.show()

If the user clicks on the point at (2, 5), what will be printed?

Matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
points = ax.scatter([1, 2, 3], [4, 5, 6], picker=True)

clicked_indices = []

def on_pick(event):
    ind = event.ind[0]
    clicked_indices.append(ind)
    print(f"Clicked point index: {ind}")

fig.canvas.mpl_connect('pick_event', on_pick)
plt.show()
AClicked point index: 0
BClicked point index: 1
CClicked point index: 2
DNo output, because picker=True is not enough
Attempts:
2 left
💡 Hint

Remember that the points are indexed in the order they are given in the scatter call.

data_output
intermediate
1:30remaining
What data is available in the pick event?

Given a matplotlib pick event handler:

def on_pick(event):
    print(event.artist)
    print(event.mouseevent.xdata, event.mouseevent.ydata)
    print(event.ind)

What will event.ind contain when picking a scatter plot?

Matplotlib
def on_pick(event):
    print(event.artist)
    print(event.mouseevent.xdata, event.mouseevent.ydata)
    print(event.ind)
AThe matplotlib figure object
BThe x and y coordinates of the picked point
CA list of indices of the picked points
DThe color of the picked point
Attempts:
2 left
💡 Hint

Check the documentation for pick_event attributes.

visualization
advanced
2:30remaining
Which code produces a plot that highlights picked points?

Which option correctly updates the color of a picked scatter point to red when clicked?

Matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
sc = ax.scatter([1, 2, 3], [4, 5, 6], picker=True)

colors = ['blue', 'blue', 'blue']
sc.set_facecolors(colors)

def on_pick(event):
    ind = event.ind[0]
    colors[ind] = 'red'
    sc.set_facecolors(colors)
    fig.canvas.draw()

fig.canvas.mpl_connect('pick_event', on_pick)
plt.show()
AUpdate colors but do not assign back to sc.set_facecolors(colors)
BChange colors[ind] to 'green' but forget to call fig.canvas.draw()
CUse sc.set_color(colors) instead of sc.set_facecolors(colors)
DThe code above
Attempts:
2 left
💡 Hint

Remember to update the scatter plot's facecolors and redraw the canvas.

🔧 Debug
advanced
2:00remaining
Why does this pick event handler not work?

Consider this code snippet:

fig, ax = plt.subplots()
points = ax.scatter([1, 2, 3], [4, 5, 6])

def on_pick(event):
    print(event.ind)

fig.canvas.mpl_connect('pick_event', on_pick)
plt.show()

Why does clicking on points not print anything?

ABecause picker=True is missing in scatter, so points are not pickable
BBecause the event handler function is not connected properly
CBecause event.ind does not exist for scatter plots
DBecause plt.show() must be called before connecting the event
Attempts:
2 left
💡 Hint

Check the scatter call parameters.

🚀 Application
expert
3:00remaining
How to implement a pick event that toggles point visibility?

You want to click on scatter points to toggle their visibility on the plot. Which code snippet correctly implements this behavior?

Matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
sc = ax.scatter([1, 2, 3], [4, 5, 6], picker=True)

visible = [True, True, True]

colors = ['blue', 'blue', 'blue']
sc.set_facecolors(colors)

def on_pick(event):
    ind = event.ind[0]
    visible[ind] = not visible[ind]
    colors[ind] = 'blue' if visible[ind] else 'none'
    sc.set_facecolors(colors)
    fig.canvas.draw()

fig.canvas.mpl_connect('pick_event', on_pick)
plt.show()
AThe code above toggles visibility by changing facecolors to 'none' and back
BChange the alpha value of the point to 0 or 1 but forget to redraw the canvas
CRemove the point from the scatter data arrays and redraw
DToggle visibility by setting the point size to 0 or original size but do not update scatter
Attempts:
2 left
💡 Hint

Changing facecolors to 'none' hides points without changing data arrays.