为什么在Python3.x上尝试使用matplotlib创建等高线图时会出现这种类型的错误?

为什么在Python3.x上尝试使用matplotlib创建等高线图时会出现这种类型的错误?,python,numpy,matplotlib,data-visualization,Python,Numpy,Matplotlib,Data Visualization,TypeError:只有长度为1的数组才能转换为Python标量 x = np.genfromtxt(CSV1, dtype=float, delimiter=',', names=True) y = np.genfromtxt(CSV2, dtype=float, delimiter=',', names=True) z = np.genfromtxt(CSV3, dtype=float, delimiter=',', names=True) def grid(x, y, z, res

TypeError:只有长度为1的数组才能转换为Python标量

x = np.genfromtxt(CSV1, dtype=float, delimiter=',', names=True) 
y = np.genfromtxt(CSV2, dtype=float, delimiter=',', names=True) 
z = np.genfromtxt(CSV3, dtype=float, delimiter=',', names=True) 

def grid(x, y, z, resX=100, resY=100):
  xi = linspace(float(min(x)), float(max(x)), resX)
  yi = linspace(float(min(y)), float(max(y)), resY)
  Z = griddata(float(x), float(y), float(z), xi, yi)
  X, Y = meshgrid(xi, yi)
  return X, Y, Z

X, Y, Z = grid(float(x), float(y), float(z))
plt.contourf(X, Y, Z)
另外,是否有一种方法可以将CSV文件中的列作为浮点数组导入,而不必显式地进行转换,如
xi=linspace(float(min(x))、float(max(x))、resX)
this?

TIA

错误源于您试图将数组强制转换为浮点。
float(np.array([1,2])
不起作用。问题是,你为什么要这么做

genfromtxt
已经创建了一个浮点数组,因此再次转换它是多余的

当然,根据数据的外观,您有不同的选择。现在让我们假设您有一个csv文件,如下所示

T1,T2,T3
20,20,20
21,22,23
22,25,25
23,21,22
你可以用英文读这个

x, y, z = np.genfromtxt("filename.csv",  delimiter=",", skip_header=1, unpack=True)
查看dtype
print(x.dtype)
显示
x
已经是一个浮点数组

然后,您可以将网格创建为

Z = griddata(x,y,z, xi, yi)
或总共

def grid(x, y, z, resX=100, resY=100):
  xi = linspace(min(x), max(x), resX)
  yi = linspace(min(y), max(y), resY)
  Z = griddata(x,y,z, xi, yi)
  X, Y = meshgrid(xi, yi)
  return X, Y, Z

X, Y, Z = grid(x, y, z)
plt.contourf(X, Y, Z)

我有一个csv文件,温度值分为3列。我将3个列拆分为3csv文件,认为这样会更容易。因此,x、y、z有一个只有一列的csv文件。现在我想将这些列存储为浮点数组,以创建网格,然后创建等高线图。我做得更好了吗?谢谢。现在可以了。默认情况下,它不会显示等高线图的颜色图例。我需要添加一些东西来展示它吗?我建议在提问之前先看看matplotlib示例。例如。