Stacked 3d bar chart with matplotlib

后端 未结 1 1504
死守一世寂寞
死守一世寂寞 2020-12-19 02:18

i worked on a simple 3d bar chart using the following code:

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

fig =         


        
1条回答
  •  有刺的猬
    2020-12-19 02:55

    To make a stacked 3d bar plot, you can accumulate your dz values and use them as the base for each next bar. Here's an example:

    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")
    
    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 bar
    colors = ['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 next
    
    plt.gca().invert_xaxis()
    plt.show()
    

    0 讨论(0)
提交回复
热议问题