emax
emax

Reputation: 7255

Python: interpolate matrix for figure

I have the following matrix R:

R
array([[ 0.014985  ,  0.01499475,  0.01508112,  0.01588764,  0.02019902, 0.03698812,  0.12376358],
       [ 0.547997  ,  0.00300703,  0.00306113,  0.00361317,  0.23311141, 0.41010791,  0.65683355],
       [ 0.7739985 ,  0.48050374,  0.00157832,  0.32448644,  0.61655571,  0.70505395,  0.82841677],
       [ 0.9547997 ,  0.89610075,  0.75911978,  0.86489729,  0.92331114, 0.94101079,  0.96568335],
       [ 0.97739985,  0.94805037,  0.87955989,  0.93244864,  0.96165557, 0.9705054 ,  0.98284168]])

That is a 5x7 matrix where the rows correspond to:

tc = [100,500,1000,5000,10000]

While the columns to the following:

y = [0,.00001, .0001, .001, .01, 0.1,  1]

If I plot R as pcolor image, I get:

z_min, z_max = -np.abs(R).max(), np.abs(R).max()
fig,ax = plt.subplots(figsize=(10,7.5))
ax.pcolor(R,cmap='RdBu',vmin=z_min, vmax=z_max)

enter image description here

I would like to interpolate the matrix in order to to have a more detailed image:

xnew,ynew = np.mgrid[0:1:1000j,0:1000:1000j]
tck = interpolate.bisplrep(tc,y,R,s=0, kx=1, ky=1)

and I get the following error:

TypeError: len(x)==len(y)==len(z) must hold.

I would like to know a way to correctly interpolate the matrix R given tc = [100,500,1000,5000,10000] and y = [0,.00001, .0001, .001, .01, 0.1, 1]

Upvotes: 1

Views: 215

Answers (1)

OMRY VOLK
OMRY VOLK

Reputation: 1481

Use zoom from scipy.ndimage.interpolation

You can increase the "zoom" until it's good enough

from scipy.ndimage.interpolation import zoom

plt.pcolor(R,cmap='RdBu')
plt.show()

R2 = zoom(R,2)
plt.pcolor(R2,cmap='RdBu')
plt.show()

R5 = zoom(R,5)
plt.pcolor(R5,cmap='RdBu')
plt.show()

R10 = zoom(R,10)
plt.pcolor(R10,cmap='RdBu')
plt.show()

You can also use different interpolation methods, see the function's docs

1

2

4

10

Upvotes: 1

Related Questions