您沒有正確使用關鍵字參數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()
來看,可以考慮使用'bar3d'方法:http://matplotlib.sourceforge.net/mpl_toolkits/mplot3d/api.html#mpl_toolkits.mplot3d.axes3d.Axes3D.bar3d – 2011-03-30 17:32:34