import matplotlib.pyplot as plt import numpy as np
#數據 T = np.array([6, 7, 8, 9, 10, 11, 12]) power = np.array([1.53E+03, 5.92E+02, 2.04E+02, 7.24E+01, 2.72E+01, 1.10E+01, 4.70E+00])
#插值 from scipy.interpolate import spline xnew = np.linspace(T.min(),T.max(),300) #300 represents number of points to make between T.min and T.max power_smooth = spline(T,power,xnew) print(xnew.shape) #(300,) print(power_smooth.shape) #(300,)
#畫圖 plt.plot(xnew,power_smooth) plt.show()
x = [0.1,0.2,……,0.9] (shape = (9))dom
y = [0.1,0.2,……,0.9] (shape = (9))ide
z = 【81個數據】(shape = (81))函數
生成數據:spa
x = np.linspace(0.1,0.9,9) y = np.linspace(0.1,0.9,9) z = np.random.rand(81)
xnew = np.arange(0.1, 1, 0.03) 【shape=(31)】
ynew = np.arange(0.1, 1, 0.03) 【shape=(31)】3d
或者code
xnew = np.linspace(0.1, 0.9, 31)blog
ynew = np.linspace(0.1, 0.9, 31)ip
x,y原數據:【x.shape=9,y.shape=9,z.shape=81】ci
f = interpolate.interp2d(x, y, z, kind='cubic')input
x,y擴充數據:【xnew.shape=31,y.shape=31】
znew = f(xnew, ynew) 【獲得的znew.shape = (31,31)】
znew爲插值後的z
Axes3D簡單用法:
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d')
好比採用plot_trisurf畫三維圖:plot_trisurf(x,y,z)
【plot_trisurf對數據要求是:x.shape = y.shape = z.shape,因此x和y的shape須要修改,採用np.meshgrid,且都爲一維數據】
#修改x,y,z輸入畫圖函數前的shape xx1, yy1 = np.meshgrid(xnew, ynew) newshape = (xx1.shape[0])*(xx1.shape[0]) y_input = xx1.reshape(newshape) x_input = yy1.reshape(newshape) z_input = znew.reshape(newshape)
# 載入模塊 import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm import pandas as pd import seaborn as sns from scipy import interpolate #生成數據 x = np.linspace(0.1,0.9,9) y = np.linspace(0.1,0.9,9) z = np.random.rand(81) #插值 # xx, yy = np.meshgrid(x, y) f = interpolate.interp2d(x, y, z, kind='cubic') xnew = np.arange(0.1, 1, 0.03) ynew = np.arange(0.1, 1, 0.03) znew = f(xnew, ynew) #修改x,y,z輸入畫圖函數前的shape xx1, yy1 = np.meshgrid(xnew, ynew) newshape = (xx1.shape[0])*(xx1.shape[0]) y_input = xx1.reshape(newshape) x_input = yy1.reshape(newshape) z_input = znew.reshape(newshape) #畫圖 sns.set(style='white') fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_trisurf(x_input,y_input,z_input,cmap=cm.coolwarm) plt.show()