Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scipy Griddata Output Dimensions

I'm not sure what I'm doing wrong. I'm attempting to use scipy griddata to interpolate data in an irregular grid.

from scipy.interpolate import griddata

I have two lists, "x" and "y", that represent the axes of my original, uninterpolated grid. They are both lists of length 8.

Then, I make the arrays that represent the axes of the intended final, filled-in grid.

ny = np.linspace(0.0, max(y), y[len(y)-1]/min_interval+1)
nx = np.linspace(0.0, max(x), len(ny))

I've checked and both "ny" and "nx" are of shape (61,). Then, I create an 8 x 8 list "z". Finally, I attempt to make my final grid.

Z = griddata((np.array(x), np.array(y)), np.array(z), (nx, ny), method='nearest', fill_value=0)
print Z.shape

The resulting 2D array has dimensions (61,8). I tried using "x" and "y" as lists and arrays - no change. Why is it only interpolating in one direction? I was expecting a (61,61) array output. I would have included actual numbers if I felt it would have been helpful, but I don't see how it would make a difference. Do I not understand how griddata works?

like image 804
adeedoubleu Avatar asked Dec 29 '25 20:12

adeedoubleu


1 Answers

Here is the full code:

import numpy as np
from scipy.interpolate import griddata

# random data to interpolate
x = np.array([0, 10, 13, 17, 20, 50, 55, 60.0])
y = np.array([10, 20, 40, 80, 90, 95, 100, 120.0])
zg = np.random.randn(8, 8)

#select one of the following two line, it depends on the order in z
#xg, yg = np.broadcast_arrays(x[:, None], y[None, :])  
xg, yg = np.broadcast_arrays(x[None, :], y[:, None]) 

yg2, xg2 = np.mgrid[y.min()-10:y.max()+10:100j, x.min()-10:x.max()+10:100j]

zg2 = griddata((xg.ravel(), yg.ravel()), zg.ravel(), (xg2.ravel(), yg2.ravel()), method="nearest")
zg2.shape = yg2.shape

import pylab as pl

pl.pcolormesh(xg2, yg2, zg2)
pl.scatter(xg.ravel(), yg.ravel(), c=zg.ravel())

the output is:

enter image description here

like image 138
HYRY Avatar answered Jan 01 '26 12:01

HYRY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!