7.3
import numpy as np import pandas as pd from scipy.interpolate import interp1d, interp2d, UnivariateSpline, griddata from scipy.optimize import least_squares, curve_fit from scipy.integrate import quad import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC'] plt.rcParams['mathtext.fontset']='stix' plt.rcParams['axes.unicode_minus']=False plt.rcParams['figure.dpi'] = 200 plt.rcParams['xtick.direction']='in' plt.rcParams['ytick.direction']='in' t0 = np.linspace(700, 780, 5) v0 = np.array([0.0977, 0.1218, 0.1406, 0.1551, 0.1664]) f1 = interp1d(t0, v0) f2 = interp1d(t0, v0, 'cubic') tp = np.array([750, 770]) vp1 = f1(tp) vp2 = f2(tp) print("线性插值的预测结果为:", vp1) print("三次样条插值的预测结果为:", vp2) t = np.linspace(700, 780, 200) v1 = f1(t) v2 = f2(t) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(t, v1, '--', label='线性插值') ax.plot(t, v2, '-', label='三次样条插值', color='#ff9900') ax.scatter(t0, v0, label='数据', zorder=10, marker='.', s=15, color='r') ax.legend(fontsize=8) fig.show() print("2022310143024")
7.4
import numpy as np import pandas as pd from scipy.interpolate import interp1d, interp2d, UnivariateSpline, griddata from scipy.optimize import least_squares, curve_fit from scipy.integrate import quad import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC'] plt.rcParams['mathtext.fontset']='stix' plt.rcParams['axes.unicode_minus']=False plt.rcParams['figure.dpi'] = 200 plt.rcParams['xtick.direction']='in' plt.rcParams['ytick.direction']='in' np.random.seed(114514) x0 = np.random.uniform(-3, 3, 50) y0 = np.random.uniform(-4, 4, 50) f = lambda x, y: (x**2 - 2*x)*np.exp(-x**2 - y**2 - x*y) z0 = f(x0, y0) xy0 = np.vstack([x0, y0]).T x = np.linspace(x0.min(), x0.max(), 300) y = np.linspace(y0.min(), y0.max(), 300) X, Y = np.meshgrid(x, y) z = griddata(xy0, z0, (X,Y), 'cubic') zl = griddata(xy0, z0, (X,Y), 'linear') z[np.isnan(z)] = zl[np.isnan(z)] fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_surface(X, Y, z, cmap='hot') fig.show() print("2022310143024")
标签:plt,rcParams,fig,第七章,ax,np,import From: https://www.cnblogs.com/yinhao3024/p/18553036