3

I have a Matplotlib figure, with some lines. I have a remove function that is called when a line is picked. The function simply removes the artist from the axes when the pick event is fired.

I would like to be able to programmatically simulate a mouse click that would trigger the pick event, if the (x, y) position of the click is exactly on (or close enough to) a line vertex.

import numpy as np
import matplotlib.pyplot as plt

def remove(event):
    event.artist.remove()

N = 10
fig, ax = plt.subplots()
for i in range(N):
    line, = ax.plot(np.random.random(2), np.random.random(2), '-o', picker=5.0)

fig.canvas.mpl_connect('pick_event', remove)

figure

I could call the remove function directly, with some (x, y) coordinates, but this would require me to know in advance which artist it corresponds to, instead of just letting Matplotlib figure that out, just like it does when I click with my mouse.

Basically, a function like simulate_pick(ax, x, y) that would:

  • do nothing if the (x, y) coords are not close enough to a line on the figure
  • call remove if (x, y) is close enough to a line
nvaytet
  • 61
  • 6

0 Answers0