A test Cursor code
- First version that recogizes both mouse and key clicks.
First version that recogizes both mouse and key clicks.
This was in: /home/sco/sco/codes/python/cursor/apr6/ct.py
#!/usr/bin/env python
''' Plot data points read from file xy.in and allow user
to identify X,Y values using the mouse '''
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Cursor
# Define the function that will store X,Y values in the
# the list coords[] when mouse clicks are detected.
def onclick(event):
global ix, iy, ib
# ix, iy, ib = event.xdata, event.ydata, event.button
ix, iy, ib = event.xdata, event.ydata, event.key
print 'x = %d, y = %d b=%s ' % (ix, iy, ib)
print 'I have x,y,button'
global coords
coords.append((ix, iy, ib))
if len(coords) == 100:
fig.canvas.mpl_disconnect(cid)
return coords
def onclick2(event):
print('%s click: button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
('double' if event.dblclick else 'single', event.button,
event.x, event.y, event.xdata, event.ydata))
# read the the X,Y plot data from file xy.in for reading
infile = open('xy.in','r')
x = []
y = []
for lines in infile:
linefull = lines.split()
x.append( linefull[0] )
y.append( linefull[1] )
# Plot the data
fig = plt.figure()
ax = fig.add_subplot(111)
#print "\nThe point style will be represented by a string. You cam use the \n"
#print "mpl command to get a full listing, but here are some examples: \n"
#print "bo- : blue circle connected by lines \n"
#print "rs : red squares with no connecting lines \n"
#pstring = raw_input("Enter point style string (bo-): ")
pstring="bo"
# pstring="rs"
ax.plot(x,y,pstring)
# Make a red, two-line cursor
cursor = Cursor(ax, useblit=True, color='red', linewidth=2)
# Set up the coords list for storing X,Y at click positions
coords = []
cid = fig.canvas.mpl_connect('button_press_event', onclick)
cid2 = fig.canvas.mpl_connect('key_press_event', onclick)
#cid = fig.canvas.mpl_connect('button_press_event', onclick2)
plt.show()
any = raw_input("Enter anything to continue: ")
# Coords is a list of lists
print "I will print coords here: "
type(coords)
nc=len(coords)
print "Number of x,y sets (mouse clicks) collected in coords = %d" % (nc)
print "\nHere are the stored mouse click events:"
for i in range(0,nc):
s = coords[i]
print "x,y,key: %9.3f %9.3f %s" % (s[0],s[1],s[2])
Back to calling page