Last active
May 22, 2018 00:37
-
-
Save stefanv/1d9e6c363a7c22c7d291debfbb5e2327 to your computer and use it in GitHub Desktop.
Capture points and replot with cubic interpolation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| from scipy import interpolate, optimize | |
| DATA = '/tmp/_coords.npy' | |
| N = 1000 | |
| P = 9 | |
| if os.path.exists(DATA): | |
| data = np.load(DATA) | |
| else: | |
| plt.xlim([1, 7]) | |
| plt.ylim([1, 4]) | |
| plt.grid() | |
| data = plt.ginput(n=-1) | |
| np.save(DATA, data) | |
| p = interpolate.CubicSpline(data[:, 0], data[:, 1]) | |
| p_min = optimize.fmin(p, x0=3) | |
| p_opt = p(p_min) | |
| x, y = data.T | |
| x_min, x_max = x.min(), x.max() | |
| t = np.linspace(x.min(), x.max(), N, endpoint=True) | |
| plt.plot(x, y, 'x', color='red') | |
| plt.annotate( | |
| 'Some text', xy=[p_min, p_opt], | |
| arrowprops={'arrowstyle': '->', 'connectionstyle': 'arc3,rad=-.2'}, | |
| xytext=[p_min + 0.7, p_opt + 0.1] | |
| ) | |
| plt.hlines(2.5, x_min, x_max) | |
| plt.plot(t, p(t)) | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment