-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot_click_example.py
More file actions
45 lines (31 loc) · 864 Bytes
/
Copy pathplot_click_example.py
File metadata and controls
45 lines (31 loc) · 864 Bytes
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
39
40
41
42
43
44
45
"""
Test to get mouse position in plot.
Examples
--------
>>> from plot_click_example import plot_click
>>> plot_click()
References
----------
http://bmi.bmt.tue.nl/~philbers/8C080/matplotlibtutorial.html
"""
from __future__ import print_function
# THIRD PARTY
import matplotlib.pyplot as plt
import numpy as np
def plot_click(data=None):
"""Plot dummy data and handle user clicks."""
# Generate data
if data is None:
data = np.arange(10000).reshape(100, 100)
# Show data
plt.imshow(data)
# Register click events
plt.connect('button_press_event', _on_click)
# Show plot
plt.show()
def _on_click(event):
"""Print and mark clicked coordinates."""
# Print data coordinates to screen
print('X, Y:', event.xdata, event.ydata)
# Mark them on plot too
plt.plot(event.xdata, event.ydata, 'rx')