Python:float()参数必须是字符串或数字,而不是';interp2d&x27;

Python:float()参数必须是字符串或数字,而不是';interp2d&x27;,python,numpy,scipy,spyder,netcdf,Python,Numpy,Scipy,Spyder,Netcdf,此代码返回错误“float()参数必须是字符串或数字,而不是‘interp2d’”。我正在尝试学习如何插值以填充数组中给定的一些值(抱歉,语法错误)。我是不是弄乱了interp2d函数的语法 import numpy as np import matplotlib.pyplot as plt from netCDF4 import Dataset import scipy as sp GCM_file = '/Users/Robert/Documents/Python Scripts/GCMf

此代码返回错误“float()参数必须是字符串或数字,而不是‘interp2d’”。我正在尝试学习如何插值以填充数组中给定的一些值(抱歉,语法错误)。我是不是弄乱了interp2d函数的语法

import numpy as np
import matplotlib.pyplot as plt
from netCDF4 import Dataset
import scipy as sp

GCM_file = '/Users/Robert/Documents/Python Scripts/GCMfiles/ATM_echc0003_1979_2008.nc'
fh = Dataset(GCM_file, mode = 'r')

pressure = fh.variables['lev'][:]
lats = fh.variables['lat'][:]
temp = np.mean(fh.variables['t'][0,:,:,:,:], axis = (0, 3))
potential_temp = np.zeros((np.size(temp,axis=0), np.size(temp,axis=1)))

P0 = pressure[0] 
#plt.figure(0)
for j in range(0, 96):
    potential_temp[:,j] = temp[:, j] * (P0/ pressure[:]) ** .238


potential_temp_view = potential_temp.view()

temp_view = temp.view()

combo_t_and_pt = np.dstack((potential_temp_view,temp_view))
combo_view = combo_t_and_pt.view()

pt_and_t_flat=np.reshape(combo_view, (26*96,2))
t_flat = temp.flatten()
pt_flat = potential_temp.flatten()

temp_grid = np.zeros((2496,96))

for j in range(0, 2496):
    if j <= 95:
        temp_grid[j,j] = t_flat[j]
    else:
            temp_grid[j, j % 96] = t_flat[j]

'''Now you have the un-interpolated grid of all your values of t as a function of potential temp and latitude, so you have to interpolate the rest somehow....?'''

xlist = lats
ylist = pt_flat

X,Y = np.meshgrid(xlist,ylist)



temp_cubic = sp.interpolate.interp2d(xlist,ylist, temp_grid, kind = 'cubic')
#temp_linear= griddata(temp_grid, (X,Y), method = 'linear')
#temp_quintic = griddata(temp_grid, (X,Y), method = 'cubic')

plt.figure(0)
plt.contourf(X,Y, temp_cubic, 20)
将numpy导入为np
将matplotlib.pyplot作为plt导入
从netCDF4导入数据集
将scipy作为sp导入
GCM_file='/Users/Robert/Documents/Python Scripts/GCMfiles/ATM_echc003_1979_2008.nc'
fh=数据集(GCM_文件,模式='r')
压力=fh.变量['lev'][:]
lats=fh.变量['lat'][:]
温度=np.平均值(fh.变量['t'][0,:,:,:,:,:],轴=(0,3))
电位温度=np.0((np.size(温度,轴=0),np.size(温度,轴=1)))
P0=压力[0]
#plt.图(0)
对于范围(0,96)内的j:
电位温度[:,j]=温度[:,j]*(P0/压力[:])**.238
潜在温度视图=潜在温度视图()
临时视图=临时视图()
组合t和pt=np.dstack((潜在临时视图,临时视图))
组合视图=组合视图
pt_和_t_平面=np.重塑(组合视图,(26*96,2))
t_展平=温度展平()
pt_flat=电势_温度展平()
温度网格=np.零((2496,96))
对于范围(02496)内的j:

如果j则问题在下一行。interp2d返回插值函数。但是,您使用它来代替countourfZ参数,它应该是一个浮点矩阵。有关详细信息,请参阅

特别是:

contour(X,Y,Z,N)
make a contour plot of an array Z.
X, Y specify the (x, y) coordinates of the surface
X and Y must both be 2-D with the same shape as Z,
  or they must both be 1-D such that
  len(X) is the number of columns in Z and
  len(Y) is the number of rows in Z.
contour up to N automatically-chosen levels.
简而言之,我认为您希望将函数应用于X和Y,以生成作为第三个参数传入的数组


matplotlib文档和kindall都显示了我的其他可能性的概念错误。

请给出实际的回溯以缩小搜索范围。
interp2d
声明:“此类返回一个函数,其调用方法使用样条插值来查找新点的值。”因此,您正在传递此函数,其中
tourtf
需要一个浮点值。@kindall当我注释掉tourtf部分时,错误消失,但没有创建temp_cubic。所以还是有问题。编辑:我看起来很愚蠢。我希望出于某种原因会弹出一个数组,而不是函数。让我改变一下,看看会发生什么,谢谢。我没说问题出在执行
contourf
调用本身。问题是
tourtf
调用的第三个参数需要一个数字,而您给了它一个函数。欢迎使用StackOverflow。请阅读并遵循帮助文档中的发布指南。适用于这里。在您发布代码并准确描述问题之前,我们无法有效地帮助您。具体来说,发布一个重现问题的示例,并给出完整的错误消息。这将包括追溯。你肯定是对的;然而,我改变了这一点,我仍然得到一个错误。编辑我的文章,如果你有兴趣看
contour(X,Y,Z,N)
make a contour plot of an array Z.
X, Y specify the (x, y) coordinates of the surface
X and Y must both be 2-D with the same shape as Z,
  or they must both be 1-D such that
  len(X) is the number of columns in Z and
  len(Y) is the number of rows in Z.
contour up to N automatically-chosen levels.