-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
/
coords_demo.py
38 lines (28 loc) · 906 Bytes
/
coords_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
===========================
Mouse move and click events
===========================
An example of how to interact with the plotting canvas by connecting to move
and click events.
"""
from matplotlib.backend_bases import MouseButton
import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0.0, 1.0, 0.01)
s = np.sin(2 * np.pi * t)
fig, ax = plt.subplots()
ax.plot(t, s)
def on_move(event):
if event.inaxes:
# get the x and y pixel coords
x, y = event.x, event.y
ax = event.inaxes # the axes instance
print('data coords %f %f, pixel coords %f %f'
% (event.xdata, event.ydata, x, y))
def on_click(event):
if event.button is MouseButton.LEFT:
print('disconnecting callback')
plt.disconnect(binding_id)
binding_id = plt.connect('motion_notify_event', on_move)
plt.connect('button_press_event', on_click)
plt.show()