I am using the mpldatacursor library to click on points on a scatter plot and store the values into a list for later use. This seems to be working fine but I also would like to see the values that I've clicked on in the scatter plot. I am using this example from Stack Overflow: Python: get corresponding information of data points interactively with mouse. The problem I am having is that the values shown when clicked are unreadable and sometimes doesn't even show up. The code is roughly the same as shown in the example, so I don't understand why, in my case, the values aren't readable. Could this possibly be because I need to update matplotlib to the newest version? Any suggestions would be appreciated.
import matplotlib.pyplot as plt
from mpldatacursor import datacursor
%matplotlib nbagg
import random
import numpy as np
fig, ax = plt.subplots()
ax.set_title('Double click on a dot to display its label')
# Plot a number of random dots
for i in range(1, 1000):
x = random.random()
y = random.random()
ax.scatter([x], [y], label='ID: X: {:.4f},Y: {:.4f}'.format(x,y))
# Use a DataCursor to interactively display the label for a selected line...
datacursor(formatter='{label}'.format)
coords = []
def onclick(event):
global ix, iy
ix, iy = event.xdata, event.ydata
print ('x = %d, y = %d'%(
ix, iy))
global coords
coords.append((ix, iy))
if len(coords) == 3:
fig.canvas.mpl_disconnect(cid)
return coords
cid = fig.canvas.mpl_connect('button_press_event', onclick)