nvaytet
nvaytet

Reputation: 71

Simulate click event in Matplotlib that triggers a pick event

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:

Upvotes: 3

Views: 287

Answers (0)

Related Questions