-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathinteractive_plots2.py
More file actions
53 lines (34 loc) · 1.21 KB
/
interactive_plots2.py
File metadata and controls
53 lines (34 loc) · 1.21 KB
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
46
47
48
49
50
51
52
import os
import numpy
from matplotlib import pyplot
def main():
InteractivePlot()
class InteractivePlot(object):
def __init__(self):
### initializing figure
self.fig = pyplot.figure()
self.ax = self.fig.add_subplot(111)
self.ax.plot(range(10), range(100,110))
### connecting figure to interactive method(s)
cid = self.fig.canvas.mpl_connect('button_press_event', self.onClick_saveToFile)
self.ax.set_xlim(0, 10)
self.ax.set_ylim(100, 110)
pyplot.show()
def onClick_saveToFile(self, event):
xdata = event.xdata
ydata = event.ydata
### initialize/open the output text file
if not os.path.exists('./output_data.csv'):
fopen = open('./output_data.csv', 'w')
fopen.write('xdata,ydata\n')
else:
fopen = open('./output_data.csv', 'a')
### plot an 'x' at each location and refresh plot window
x = self.ax.plot(xdata, ydata, marker='x', ms=10, color='k')
pyplot.draw()
### write to output file
fopen.write('%.2f,%.2f' % (xdata, ydata))
fopen.write('\n')
fopen.close()
if __name__ == '__main__':
main()