2011-03-30 104 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