2011-03-30 105 views
1

我在scipy/numpy中有一个Nx3矩阵,我想制作一个三维条形图,其中X和Y轴由第一个值和矩阵的第二列,每个条的高度是矩阵中的第三列,并且条的数量由N确定。用matplotlib在Python中的三维直方图的怪异行为

此外,我想绘制几组这些矩阵,每个矩阵都不同颜色(一个 “分组” 3D条形图。)

当我尝试如下绘制它:

ax.bar(data[:, 0], data[:, 1], zs=data[:, 2], 
       zdir='z', alpha=0.8, color=curr_color) 

我得到真正奇怪的酒吧 - 如看到这里:http://tinypic.com/r/anknzk/7

任何想法为什么酒吧是如此歪曲和怪异的形状?我只想在X-Y点上有一个杆,其高度等于Z点。

+0

来看,可以考虑使用'bar3d'方法:http://matplotlib.sourceforge.net/mpl_toolkits/mplot3d/api.html#mpl_toolkits.mplot3d.axes3d.Axes3D.bar3d – 2011-03-30 17:32:34

回答

2

您没有正确使用关键字参数zs。它指的是每组钢筋放置的平面(沿着轴线zdir定义)。它们是歪曲的,因为它假定由ax.bar呼叫定义的一组条形线在同一平面上。你可能多次打电话ax.bar多次(每架飞机一个)。密切关注this example。您需要zdir'x''y'

编辑

这里是全码(主要基于上面链接的示例)。在文档

from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 
import numpy as np 

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

# this is just some setup to get the data 
r = numpy.arange(5) 
x1,y1 = numpy.meshgrid(r,r) 
z1 = numpy.random.random(x1.shape) 

# this is what your data probably looks like (1D arrays): 
x,y,z = (a.flatten() for a in (x1,y1,z1)) 
# preferrably you would have it in the 2D array format 
# but if the 1D is what you must work with: 
# x is: array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 
#    0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 
#    0, 1, 2, 3, 4]) 
# y is: array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 
#    2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 
#    4, 4, 4, 4, 4]) 

for i in range(0,25,5): 
    # iterate over layers 
    # (groups of same y) 
    xs = x[i:i+5] # slice each layer 
    ys = y[i:i+5] 
    zs = z[i:i+5] 
    layer = ys[0] # since in this case they are all equal. 
    cs = numpy.random.random(3) # let's pick a random color for each layer 
    ax.bar(xs, zs, zs=layer, zdir='y', color=cs, alpha=0.8) 

plt.show() 
+0

你是什么为每架飞机打电话一次?你能举一个例子吗?我仍然无法使其工作 – user248237dfsf 2011-03-30 18:33:55

+0

@ user248237。看我的编辑。 – Paul 2011-03-30 18:56:22

+0

谢谢。我怎样才能在3D空间中的每个轴上设置标签?谢谢 – user248237dfsf 2011-03-30 19:21:03