2015-02-06 95 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