contour plot from csv file using interpolation : results in NaN

Viewed 122

I am trying to produce a contour plot using axis 0, 1, and 3 (x, y, and z). However, I am having trouble to interpolating the z-values on the grid.

The data is:

0.8917  3.3825  7.755   0.3709
3.2707  3.3706  8.0704  0.5669
4.6345  0.5754  7.9279  0.5383
1.4717  0.8609  7.9363  0.5256
9.1615  3.6124  7.8407  0.5833
10.613  0.8782  7.8781  0.537
7.5193  0.768   7.8407  0.5833
-2.2248 8.29    7.6937  0.3578
0.207   8.5664  7.973   0.5345
1.9296  5.8915  8.0567  0.534
-1.2482 6.0459  7.973   0.5345
3.5835  8.0449  7.755   0.371
6.0573  8.8033  7.9363  0.5256
7.8859  6.2071  7.9279  0.5383
4.7833  5.9905  8.0704  0.5669
12.0913 0.0247  8.4997  -0.9643
0.0852  10.2797 8.5307  -0.9526
-2.7929 5.2947  8.5307  -0.9526
3.3877  5.0497  8.7554  -0.9357
3.1277  1.603   7.5125  -1.007
9.0397  1.7865  7.4696  -0.9899
0.3865  6.7824  7.4898  -1.0024
6.2426  6.9982  7.5125  -1.007
-0.02   3.5714  6.0746  -0.9935
6.0881  3.4906  6.3915  -0.9577
-3.0798 8.7837  6.0443  -0.9931

Here I am trying to produce the 2D grid and interpolate the z-values in the grid:

x_y_col = contour_data[[0,1]].to_numpy()

points = x_y_col

values =contour_data[[3]].to_numpy()
x = np.linspace(-5,10,10)
y = np.linspace(0,10,10)
grid_x,grid_y = np.meshgrid(x,y)
Z= griddata(points, values, (grid_x, grid_y), method='cubic')

However, when I tried to print the interpolated data it returns some of the NaN values like this:

[[[        nan]
  [        nan]
  [        nan]
  [        nan]
  [        nan]
  [        nan]
  [        nan]
  [        nan]
  [        nan]
  [        nan]]

 [[        nan]
  [        nan]
  [        nan]
  [        nan]
  [ 0.20175338]
  [-0.78055115]
  [ 0.01861935]
  [ 0.29941125]
  [-0.21535268]
  [ 0.13910528]]

 [[        nan]
  [        nan]
  [        nan]
  [        nan]
  [ 0.19295743]
  [-0.44071375]
  [-0.85967443]
  [-0.71360517]
  [-1.15037581]
  [ 0.40896608]]

 [[        nan]
  [        nan]
  [        nan]
  [-0.9840817 ]
  [ 0.76255707]
  [ 0.54118907]
  [-0.54032303]
  [-1.02423615]
  [-0.42427756]
  [        nan]]

Is there something wrong with my code? The Z values also need to be in 2D array for me to plot it

1 Answers

The documentation page of griddata describes the keyword fill_value:

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.

What is happening is that griddata can only provide interpolated values within the polygon that encompasses the input points you supply. Any attempt to interpolate outside of this polygon will use the value held by fill_value, which is nan by default. You have at least three possible strategies:

  • Leave as is
  • Provide a value to fill_value
  • Use the nearest interpolation scheme, which does not rely on the convex hull of the input points

All three strategies are illustrated below, where so.txt contains exactly the data you included in the OP.

import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import griddata

data = np.loadtxt('so.txt')
gridY, gridX = np.mgrid[0:10:11j, -5:10:17j]
leave = griddata(data[:, :2], data[:, 3], (gridX, gridY), method='cubic')
fill_value = griddata(data[:, :2], data[:, 3], (gridX, gridY), method='cubic',
                      fill_value=0)
nearest = griddata(data[:, :2], data[:, 3], (gridX, gridY), method='nearest')
fig, (ax, bx, cx) = plt.subplots(ncols=3, figsize=(16, 8))
ax.contourf(gridX, gridY, leave)
ax.set_title('Cubic')
bx.contourf(gridX, gridY, fill_value)
bx.set_title('Cubic, fill_value = 0')
ctrf = cx.contourf(gridX, gridY, nearest)
cx.set_title('Nearest')
fig.colorbar(ctrf, cax=fig.add_axes([0.11, 0.95, 0.8, 0.04]),
             orientation='horizontal')
plt.show()

enter image description here

Related