2
我很難在python中插入矩陣/數據幀。Python:是否可以在MATLAB風格中插入矩陣?
假設我們我有一個矩陣M = 3x4
和x = [1 3 5]
,y = [0.1 0.4 0.5 0.7]
這是我的方式做插值,然後在Matlab繪製。
xq = 1:1:5;
yq = 0.1:0.1:1;
[xq,yq] = meshgrid(xq,yq);
zq = interp2(y,x,M,xq,yq);
figure
h=pcolor(xq,yq,zq)
set(h,'EdgeColor','none')
這是在Python
from scipy import interpolate
import numpy as np
def my_interp(X, Y, Z, x, y, spn=3):
xs,ys = map(np.array,(x,y))
z = np.zeros(xs.shape)
for i,(x,y) in enumerate(zip(xs,ys)):
# get the indices of the nearest x,y
xi = np.argmin(np.abs(X[0,:]-x))
yi = np.argmin(np.abs(Y[:,0]-y))
xlo = max(xi-spn, 0)
ylo = max(yi-spn, 0)
xhi = min(xi+spn, X[0,:].size)
yhi = min(yi+spn, Y[:,0].size)
# make slices of X,Y,Z that are only a few items wide
nX = X[xlo:xhi, ylo:yhi]
nY = Y[xlo:xhi, ylo:yhi]
nZ = Z[xlo:xhi, ylo:yhi]
intp = interpolate.interp2d(nX, nY, nZ)
z[i] = intp(x,y)[0]
return z
zq = my_interp(y, x, M, xq, yq)
是的,有:) –
一些公然的自我推銷:[插值(尋找griddata)](http://stackoverflow.com/questions/34643642/scipy-interp2d-bisplrep-unexpected-output-when-given -1d-input/34656728#34656728)和[pcolor(mesh)](http://stackoverflow.com/questions/35162798/superimpose-heat-maps-in-one-plot-in-python/35166749#35166749)。必要的功能是有據可查的。 –
您可能會描述那些不熟悉MATLAB功能的Python人員的過程和結果。 – Prune