2017-08-12 146 views
0

我有3个大量的数据。这些是x和y值以及每个xy点的温度值。我想绘制每个点并插入点之间的区域以获得连续的曲面。我遇到的问题是指定温度值。我无法使用相同数量的x,y和z(温度)值来工作,并且我可以在网上找到的所有示例都使用x和y的函数来创建z或具有xy上每个点的z值格。 有没有简单的方法来做到这一点?python中的轮廓图

import numpy as np 
import matplotlib.pyplot as plt 

fig, axs = plt.subplots() 

x = np.linspace(0, 1, 100) 
y = np.linspace(0,1,100) 
X, Y = np.meshgrid(x, y) 
#Z = np.sin(X)*np.sin(Y) # want to specify not an equation 
Z = np.linspace(1,2,100) 
levels = np.linspace(-1, 1, 40) 
cs = axs.contourf(X, Y, Z, levels=levels) 
fig.colorbar(cs, ax=axs, format="%.2f") 
plt.show() 

更新:

这是我到目前为止所。我仍然需要制定一个很好的方法来填补点之间的区域。有没有人有任何想法?

import numpy as np 
import matplotlib.pyplot as plt 

fig, axs = plt.subplots() 

# create a grid in the correct shape/size 
x = np.linspace(0, 1, 3) 
y = np.linspace(0,1,3) 
X, Y = np.meshgrid(x, y) 

# specify and change the relevent areas 
y = [1,2,0] # location of point in x direction 
x =[2,1,1] #location of point in y direction 
z = [40,30,20] #temperature 
Z = np.arange(1,10).reshape((3,3)) 
Z[y,x] = z 

levels = np.linspace(0, 40, 40) 
cs = axs.contourf(X, Y, Z, levels=levels) 
fig.colorbar(cs, ax=axs, format="%.2f") 
plt.show() 
+0

设置网格只写'Z = np.zeros(X.shape)'和这条线将创建后具有X阵列形状的Empry Z阵列。 – Serenity

+0

这个问题有答案https://stackoverflow.com/questions/3242382/interpolation-over-an-irregular-grid – Robin

回答

0

人们使用x和y函数的原因是因为您的Z值必须是x和y的函数。在您的测试代码中,Z是1D,但它需要2D来绘制轮廓。

如果您有Z(温度)值与您的x和y坐标具有相同的形状,那么它应该工作。

x = np.linspace(0, 1, 100) 
y = np.linspace(0,1,100) 

X, Y = np.meshgrid(x, y) 
#Z = np.sin(X)*np.sin(Y) # want to specify not an equation 
Z = np.linspace(1,2,100) 

print X.shape 

print Z.shape 

(100L,100L)

(100L)