Python 3D堆积条形图

时间:2016-02-11 18:20:30

标签: python matplotlib plot

我希望制作一个三维条形图,如herehere所示,但有堆叠条形图,如here所示。

任何人都知道如何在同一个情节中做这两个?

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以在三维条形图中设置bottom,与二维相同:

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')
colors = ['r', 'g', 'b', 'y']
for i, (c, z) in enumerate(zip(colors, [30, 20, 10, 0])):
    xs = np.arange(20)
    ys = np.random.rand(20)
    ys2 = np.random.rand(20)
    ys3 = np.random.rand(20)

    # You can provide either a single color or an array. To demonstrate this,
    # the first bar of each set will be colored cyan.
    cs = [c] * len(xs)
    cs[0] = 'c'
    ax.bar(xs, ys, zs=z, zdir='y', color=cs, alpha=0.8)
    ax.bar(xs, ys2, bottom=ys, zs=z, zdir='y', color=colors[(i+1)%4], alpha=0.8)
    ax.bar(xs, ys3, bottom=ys+ys2, zs=z, zdir='y', color=colors[(i+2)%4], alpha=0.8)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

enter image description here

答案 1 :(得分:1)

您也可以使用bar3d:zpos参数允许您设置条形图的底部。这里有一个基于matplotlib示例(hist3d_demo.py

的演示代码的示例
<script src="<a href='https://...'>script.js</a>"></script>

Figure with the 3-D bar chart

相关问题