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 |
import os import matplotlib.pyplot as plt import numpy as np from scipy import interpolate os.chdir(r'D:\data\scripts\web1\ex27') os.getcwd() x = np.array([0, 1, 2, 3, 4, 5, 6, 7]) y = np.array([3, 4, 3.5, 2, 1, 1.5, 1.25, 0.9]) xx = np.linspace(x.min(), x.max(), 100) fig, ax = plt.subplots(figsize=(8, 4)) ax.scatter(x, y) for n in [1, 3, 5, 7]: f = interpolate.interp1d(x, y, kind=n) ax.plot(xx, f(xx), label='order %d' % n) ax.legend() ax.set_ylabel(r"$y$", fontsize=18) ax.set_xlabel(r"$x$", fontsize=18) fig.tight_layout() ax.set_title("Spline Interpolation") fig.tight_layout() plt.savefig("example27.png", dpi=100) plt.show() plt.close() <img class=" wp-image-134 aligncenter" src="https://gantovnik.com/bio-tips/wp-content/uploads/2019/01/example27.png" alt="example27" width="632" height="316" /> |
Recent Comments