How to interpolate only zeros in 2D array without change nonzero values using scipy griddata?

时间:2019-01-18 18:51:11

标签: python-2.7

I have 2D zeros array MxN (M is not equal to N). I placed some values in the array and now i would like to interpolate the missing data (the zeros). i'm trying to use scipy griddata but the result of my code change dramatically the values (max value = 80 turn to 40). my question is, whats wrong with my code? what am i missing? Please see my code below.

import numpy as np
import scipy
from scipy.interpolate import griddata

R = np.linspace(0.0, 10, num=100)  #those are the columns
T = np.arange(37, 80, 1)           #those are the rows
grid_x, grid_y = np.meshgrid(T,R) #those are the new grid for interpolation 

#data_interpolated is the zeros matrix with my data

tmp_float = data_interpolated.astype(float, order='K')

points = np.array(np.nonzero(tmp_float))  #find nonzeros values indexs
values = tmp_float[np.nonzero(tmp_float)] #find the values in those indexs

#interpolate
interpolated_data = griddata(points.transpose(), values, (grid_x, grid_y), 
...method='linear')

0 个答案:

没有答案