Reputation: 23
Edit: ax.scatter(event.xdata, event.ydata)
works fine, IDK how I went past this. However, pressing the button is still paint dots and I'm wondering if filtering coordinates is good practice to solve this.
Here is my minimal code to plot some points on axis. Unfortunately, with the button code plt.scatter()
stops working properly (dots start to appear on top of the button).Any advice for me?
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim([0, 10])
ax.set_ylim([0, 10])
def onclick(event):
plt.scatter(event.xdata, event.ydata)
fig.canvas.draw()
cid = fig.canvas.mpl_connect('button_press_event', onclick)
def do(event):
print(1)
bprev = Button(plt.axes([0.7, 0.05, 0.1, 0.075]), 'Previous')
bprev.on_clicked(do)
plt.show()
Upvotes: 1
Views: 469
Reputation: 69106
To solve the first problem, as Mr T. pointed out, you can use ax.scatter(event.xdata, event.ydata)
to plot on the main axes, not on the button.
To solve the problem of points being plotted when the button is clicked, you can use the event.inaxes
property, which tells you the Axes instance of the mouse click. So, if event.inaxes
is the same as ax
, draw the point; if not, then it is in the button, so do nothing.
In your function, it would look like this:
def onclick(event):
if event.inaxes == ax:
ax.scatter(event.xdata, event.ydata)
fig.canvas.draw()
Upvotes: 2