在python中使用matplotlib制作3D线框时遇到问题

时间:2015-02-06 18:42:37

标签: python matplotlib

所以我有一个数据集,我试图将其合并到一个矩阵中,然后制作一个线框图。当我显示绘图时,所有显示的是沿着3d图像的x = y线的平面。我想要完整的矩阵显示。我已经包含了我的代码以及stats.txt的示例:

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

1 个答案:

答案 0 :(得分: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)