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?
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()
Remember that the points are indexed in the order they are given in the scatter call.
The points are plotted in order: (1,4) index 0, (2,5) index 1, (3,6) index 2. Clicking on (2,5) triggers the pick event with index 1.
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?
def on_pick(event): print(event.artist) print(event.mouseevent.xdata, event.mouseevent.ydata) print(event.ind)
Check the documentation for pick_event attributes.
For scatter plots, event.ind is a list of indices of the points that were picked.
Which option correctly updates the color of a picked scatter point to red when clicked?
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()
Remember to update the scatter plot's facecolors and redraw the canvas.
Only option D correctly updates the facecolors list, assigns it back, and redraws the canvas to show the change.
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?
Check the scatter call parameters.
Without picker=True, matplotlib does not generate pick events for the scatter points.
You want to click on scatter points to toggle their visibility on the plot. Which code snippet correctly implements this behavior?
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()
Changing facecolors to 'none' hides points without changing data arrays.
Option A correctly toggles visibility by changing facecolors and redrawing the canvas, which is efficient and simple.