2015-02-06 91 views
0

所以我有一个数据集,我试图把它放入一个矩阵中,然后制作一个线框图。当我显示该图时,所有显示的是沿着三维图像的x = y线的平坦表面。我想要显示完整的矩阵。我已经包括了我的代码,以及在stats.txt的样本:在Python中使用matplotlib制作3D线框时遇到困难

from numpy import * 
from pylab import * 
f = open('stats.txt') 

bins = 10 

xs = [] 
ys = [] 

for line in f: 
     line = line.strip().split(' ') 
     xs.append(float(line[0])) 
     ys.append(float(line[1])) 
xlin = linspace(min(xs),max(xs),bins+1) 
ylin = linspace(min(ys),max(ys),bins+1) 

matrix = zeros((bins,bins)) 

for i in range(bins): 
     for j in range(bins): 
       count = 0 
       for s in range(len(xs)): 
         if xs[s] >= xlin[i] and xs[s] <= xlin[i+1] and ys[s] >= ylin[j] and ys[s] <= ylin[j+1]: 
           count +=1 
       matrix[i,j] = count 
print matrix 

x = [] 
y = [] 
for i in range(bins): 
     x.append([0.,1.,2.,3.,4.,5.,6.,7.,8.,9.]) 

for i in range(bins): 
     y.append([0.,1.,2.,3.,4.,5.,6.,7.,8.,9.]) 
#for i in range(bins): 
#  y.append(linspace(0,bins-1,bins)) 



import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d.axes3d import Axes3D 

fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 

print shape(x) 
print shape(y) 
print shape(matrix) 
ax.plot_wireframe(x, y, matrix) 

#plt.imshow(matrix,cmap=plt.cm.ocean) 
plt.show() 

stats.txt样本:

10385.8694574 114.758131279 
11379.8955938 -166.830995639 
10347.5572407 165.168099188 
11698.0834105 110.188708959 
12100.3323331 185.316597413 
11530.3943217 287.99795812 
11452.2864796 474.890116234 
12181.4426414 149.266756079 
10962.8512477 -544.794117131 
10601.2128384 49.782478266 

回答

0

与您的代码的问题是,你的x坐标是与每个数据点的y坐标相同。因此,你正在有效地告诉matplotlib你在x-y平面上只有对角线上的值。

一个可能的解决方案是简单地转置你的y坐标。但是,使用numpy的meshgridlink)函数可能会更舒适。

x,y = np.meshgrid(np.arange(bins),np.arange(bins)) 
fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 
ax.plot_wireframe(x, y, matrix) 
+0

这正是我一直在寻找,非常感谢你 编辑:试图把票投给这个答案,但它不会让我因缺少点。再次感谢! – user3817123 2015-02-06 19:41:44