This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
python:def [2015/11/18 13:48] admin created |
python:def [2021/10/25 08:53] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | {{: | ||
+ | |||
<file python def.py> | <file python def.py> | ||
# | # | ||
Line 17: | Line 19: | ||
figure() | figure() | ||
plot(x+b/ | plot(x+b/ | ||
+ | |||
+ | show() | ||
+ | </ | ||
+ | |||
+ | <file python def2.py> | ||
+ | # | ||
+ | |||
+ | from pylab import * | ||
+ | |||
+ | y, x = loadtxt(" | ||
+ | x = -x | ||
+ | |||
+ | plot(x,y) | ||
+ | |||
+ | data = array(ginput(2)) | ||
+ | |||
+ | a,b = sort(data[:, | ||
+ | c = b-a | ||
+ | xx = array([a-c/ | ||
+ | |||
+ | m = (x>=a) & (x<=b) | ||
+ | |||
+ | plot(x[m], y[m], lw=5) | ||
+ | |||
+ | a,b = polyfit(x[m], | ||
+ | |||
+ | plot(xx, | ||
+ | |||
+ | figure() | ||
+ | plot(x+b/ | ||
+ | |||
+ | show() | ||
+ | </ | ||
+ | |||
+ | <file python def3.py> | ||
+ | # | ||
+ | |||
+ | from pylab import * | ||
+ | |||
+ | class Point: | ||
+ | def __init__(self, | ||
+ | self.callback = callback | ||
+ | self.line = axvline(pos, | ||
+ | self.pos = pos | ||
+ | self.active = False | ||
+ | |||
+ | connect(' | ||
+ | connect(' | ||
+ | connect(' | ||
+ | |||
+ | def on_press(self, | ||
+ | if self.line.contains(e)[0]: | ||
+ | self.active = True | ||
+ | self.on_motion(e) | ||
+ | |||
+ | def on_motion(self, | ||
+ | if not self.active: | ||
+ | return | ||
+ | self.line.set_xdata(e.xdata) | ||
+ | if self.callback: | ||
+ | self.pos = e.xdata | ||
+ | self.callback() | ||
+ | else: | ||
+ | self.line.figure.canvas.draw() | ||
+ | |||
+ | def on_release(self, | ||
+ | self.on_motion(e) | ||
+ | self.active = False | ||
+ | |||
+ | |||
+ | class LineFitter: | ||
+ | |||
+ | def __init__(self, | ||
+ | self.ax = figure().gca() | ||
+ | self.ax.plot(xdata, | ||
+ | |||
+ | self.a = Point(apos, self.callback) | ||
+ | self.b = Point(bpos, self.callback) | ||
+ | self.xdata = xdata | ||
+ | self.ydata = ydata | ||
+ | self.plot_data, | ||
+ | self.plot_fit, | ||
+ | |||
+ | self.ax2 = figure().gca() | ||
+ | self.plot_corrected, | ||
+ | |||
+ | |||
+ | def callback(self): | ||
+ | aa,bb = sort([self.a.pos, | ||
+ | m = (self.xdata> | ||
+ | |||
+ | self.plot_data.set_data(self.xdata[m], | ||
+ | |||
+ | cc = (bb-aa)/5 | ||
+ | xx = array([aa-cc, | ||
+ | |||
+ | a,b = polyfit(x[m], | ||
+ | | ||
+ | self.plot_fit.set_data(xx, | ||
+ | self.plot_corrected.set_data(self.xdata+b/ | ||
+ | | ||
+ | # | ||
+ | |||
+ | self.ax.figure.canvas.draw() | ||
+ | self.ax2.figure.canvas.draw() | ||
+ | |||
+ | y, x = loadtxt(" | ||
+ | x = -x | ||
+ | |||
+ | l = LineFitter(amax(x)*0.1, | ||
show() | show() | ||
</ | </ |