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 |
import os import matplotlib.pyplot as plt import numpy as np from numpy import polynomial as P os.chdir(r'D:\data\scripts\web1\ex24') os.getcwd() x = np.array([1, 2, 3, 4]) y = np.array([1, 3, 5, 4]) f1 = P.Polynomial.fit(x, y, 1) f2 = P.Polynomial.fit(x, y, 2) f3 = P.Polynomial.fit(x, y, 3) xx = np.linspace(x.min(), x.max(), 100) fig, ax = plt.subplots(1, 1, figsize=(8, 4)) ax.plot(xx, f1(xx), 'r', lw=2, label='1st order') ax.plot(xx, f2(xx), 'g', lw=2, label='2nd order') ax.plot(xx, f3(xx), 'b', lw=2, label='3rd order') ax.scatter(x, y, label='data points') ax.legend(loc=4) ax.set_xticks(x) ax.set_ylabel(r"$y$", fontsize=18) ax.set_xlabel(r"$x$", fontsize=18) ax.set_title("Polynomial fit") fig.tight_layout() plt.savefig("example24.png", dpi=100) plt.show() plt.close() <img class=" wp-image-125 aligncenter" src="https://gantovnik.com/bio-tips/wp-content/uploads/2019/01/example24.png" alt="example24" width="606" height="303" /> |
Recent Comments