我正在尝试使用具有4列的数据绘制3D表面图,即它将是x,y,z,c。下面显示的数据 -
0.452 -172.799 -172.800 0.000000
0.452 -172.799 -158.400 -9.305322
0.452 -172.799 -144.000 -12.062071
0.452 -172.799 -129.600 -9.008698
0.452 -172.799 -115.200 -5.402975
0.452 -172.799 -100.800 -3.957608
0.452 -172.799 -86.400 -5.113347
0.452 -172.799 -72.000 -4.784993
0.452 -172.799 -57.600 -6.659095
0.452 -172.799 -43.200 -4.405937
0.452 -172.799 -28.800 -6.110385
0.452 -172.799 -14.400 -5.896424
0.452 -172.799 -0.001 -4.405937
0.452 -172.799 14.399 -6.489442
0.452 -172.799 28.799 -5.662057
0.452 -172.799 43.199 -7.710180
0.452 -172.799 57.599 -7.710180
0.452 -172.799 71.999 -8.011659
0.452 -172.799 86.399 -6.489442
0.452 -172.799 100.799 -6.817796
0.452 -172.799 115.199 -5.662057
0.452 -172.799 129.599 -5.402975
0.452 -172.799 143.999 -5.662057
0.452 -172.799 158.399 -3.957608
0.452 -172.799 172.799 -2.701488
0.615 -172.799 -172.800 -8.444175
0.615 -172.799 -158.400 -13.183417`
前三列代表x,y,z和第四列c将是颜色条,即表面颜色将根据c给出。
现在我尝试过几个简单的代码,但仍然会出错。我试过的是 -
import pylab as p
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.cm as cm
data = np.genfromtxt('test1.pmf')
x=data[:,1]
y=data[:,2]
h=data[:,3]
z=data[:,0]
datamin=min(h)
datamax=max(h)
fig=p.figure()
ax=fig.add_subplot(111, projection='3d')
pmf = ax.plot_surface(x, y, z, facecolors=cm.ocean(h))
p.colorbar(pmf)
p.show()
我收到了诸如
之类的错误pmf = ax.plot_surface(x, y, z, facecolors=cm.ocean(h))
File "/usr/lib/pymodules/python2.7/mpl_toolkits/mplot3/axes3d.py",line663,in
plot_surface rows, cols = Z.shape ValueError: need more than 1 value to unpack
任何想法在哪里做错了?
非常感谢你!
答案 0 :(得分:0)
plot_surface
函数requires 2D arrays as an arguments。在您的代码段中,Z
变量是一维数组,因此Z.shape
无法解压缩为两个元素(行和列)。
我相信,这样的事情可能是正确的:
x, y = np.meshgrid(x, y)
z = z.reshape(x.shape)
请参阅example