要制作堆叠的3d条形图,您可以累积
dz值并将其用作下一个条形的基础。这是一个例子:
from mpl_toolkits.mplot3d import Axes3Dimport matplotlib.pyplot as pltimport numpy as npfig = plt.figure()ax = fig.add_subplot(111, projection = "3d")ax.set_xlabel("x")ax.set_ylabel("y") ax.set_zlabel("z")ax.set_xlim3d(0,10)ax.set_ylim3d(0,10)xpos = [2,5,8,2,5,8,2,5,8]ypos = [1,1,1,5,5,5,9,9,9]zpos = np.zeros(9)dx = np.ones(9)dy = np.ones(9)dz = [np.random.random(9) for i in range(4)] # the heights of the 4 bar sets_zpos = zpos # the starting zpos for each barcolors = ['r', 'b', 'g', 'y']for i in range(4): ax.bar3d(xpos, ypos, _zpos, dx, dy, dz[i], color=colors[i]) _zpos += dz[i] # add the height of each bar to know where to start the nextplt.gca().invert_xaxis()plt.show()


