Scipy griddata with linear and cubic yields nan

2024/9/29 17:31:19

the following code should produce griddata. But in case I choose as interpolation type 'cubic' or 'linear' I am getting nan's in the z grid. Wen i'm choosing 'nearest' everything is running fine. Here is an example code:

import numpy as np
from scipy.interpolate import griddatax = np.array([0.03,0.05,0033])
y = np.array([0.004,0.01,0.02])
z = np.array([1,2,3])xy = np.zeros((2,np.size(x)))
xy[0] = x
xy[1] = y
xy = xy.Tgrid_x, grid_y = np.mgrid[0.0:0.09:250*1j, 0.0:0.03:250*1j] #generating the gridi_type= 'cubic' #nearest, linear, cubic
grid_z = griddata(xy, z, (grid_x, grid_y), method=i_type)#check if there is a nan in the z grid:
print np.isnan(grid_z).any()

I don't have any idea why this is not working..

Answer

Your area you look at is simply much larger than your input points. This doesn't matter for 'nearest' as this always puts the nearest value to a certain coordinate. But 'linear' and 'cubic' do not extrapolate but fill the values which are not within the input area with nan by default.

See also the docs of griddata:

fill_value : float, optional
Value used to fill in for requested points outside of the convex hull of the input points. If not provided, then the default is nan. This option has no effect for the ‘nearest’ method.

Best understandable when plotted by imshow:

enter image description here

plot created with:

import numpy as np
from scipy.interpolate import griddatax = np.array([0.03,0.05,0.033])
y = np.array([0.004,0.01,0.02])
z = np.array([1,2,3])xy = np.zeros((2,np.size(x)))
xy[0] = x
xy[1] = y
xy = xy.Tgrid_x, grid_y = np.mgrid[0.0:0.09:250*1j, 0.0:0.03:250*1j] #generating the gridfig, axs = plt.subplots(3)
for i, i_type in enumerate(['cubic', 'nearest', 'linear']): #, cubicgrid_z = griddata(xy, z, (grid_x, grid_y), method=i_type)#check if there is a nan in the z grid:axs[i].imshow(grid_z)axs[i].set_title(i_type)plt.tight_layout()
https://en.xdnf.cn/q/71188.html

Related Q&A

Clone a module and make changes to the copy

Is it possible to copy a module, and then make changes to the copy? To phrase another way, can I inherit from a module, and then override or modify parts of it?

AWS Lambda, Python, Numpy and others as Layers

I have been going at this for a while trying to get python, numpy and pytz added to AWS Lambda as Layers rather than having to zip and throw it at AWS with my .py file. I was able to follow multiple tu…

Is there a way to check if a module is being loaded by multiprocessing standard module in Windows?

I believe on Windows, because there is no fork, the multiprocessing module reloads modules in new Pythons processes. You are required to have this code in your main script, otherwise very nasty crashes…

Condas solving environment takes forever

I am using conda since one year, since several weeks, whenever I want to install a package using conda install -c anaconda <package_name>, for any package, it is just stuck at the Solving environ…

How to overwrite a file in Python?

Im trying to overwrite a file. I based my answer on this Read and overwrite a file in PythonTo complete my codes:<select class="select compact expandable-list check-list" ONCHANGE="lo…

Is os.popen really deprecated in Python 2.6?

The on-line documentation states that os.popen is now deprecated. All other deprecated functions duly raise a DeprecationWarning. For instance:>>> import os >>> [c.close() for c in os…

Routes with trailing slashes in Pyramid

Lets say I have a route /foo/bar/baz. I would also like to have another view corresponding to /foo or /foo/. But I dont want to systematically append trailing slashes for other routes, only for /foo a…

understanding item for item in list_a if ... PYTHON

Ive seen the following code many times, and I know its the solution to my problems, but Im really struggling to understand HOW it works. The code in particular is:item for item in list_a if item not in…

Django redirect to custom URL

From my Django app, how to I redirect a user to somescheme://someurl.com?To give you some context in case it helps, I have a working oauth2 server written in Python/Django and I need to allow users to…

Python embedding with threads -- avoiding deadlocks?

Is there any way to embed python, allow callbacks from python to C++, allowing the Pythhon code to spawn threads, and avoiding deadlocks?The problem is this:To call into Python, I need to hold the GIL…