Python 3D 堆积条形图

标签 python matplotlib plot

我想要制作一个 3D 条形图,如图 herehere ,但有堆叠条,如 here 所示。

有人知道如何在同一个情节中完成这两个任务吗?

谢谢!

最佳答案

您还可以使用 bar3d 来完成此操作:zpos 参数允许您设置条形的底部。这里是一个基于 matplotlib 示例中的演示代码的示例 ( hist3d_demo.py )

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')
x, y = np.random.rand(2, 100) * 4
hist, xedges, yedges = np.histogram2d(x, y, bins=4, range=[[0, 4], [0, 4]])

# Construct arrays for the anchor positions of the 16 bars.
# Note: np.meshgrid gives arrays in (ny, nx) so we use 'F' to flatten xpos,
# ypos in column-major order. For numpy >= 1.7, we could instead call meshgrid
# with indexing='ij'.
xpos, ypos = np.meshgrid(xedges[:-1] + 0.25, yedges[:-1] + 0.25)
xpos = xpos.flatten('F')
ypos = ypos.flatten('F')
# z position set to zero
zpos = np.zeros_like(xpos)

# Construct arrays with the dimensions for the 16 bars.
dx = 0.5 * np.ones_like(zpos)
dy = dx.copy()
dz = hist.flatten()
# Create a random second histogram
dz2 = dz * np.random.rand(16)
# Set z position to the values of the first histogram
zpos2 = dz

ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b', zsort='average', alpha=0.7)
ax.bar3d(xpos, ypos, zpos2, dx, dy, dz2, color='r', zsort='average', alpha=0.7)

plt.show()

Figure with the 3-D bar chart

关于Python 3D 堆积条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35347091/

相关文章:

python - matplotlib 高级条形图

python - pandas:如何在 df.plt() 中很好地格式化时间戳轴标签?

haskell - 无法成功安装plot Haskell

r - 使用 ggplot2 的 Hodogram

python - 打印带有后缀文本的左对齐格式化 float

python - 检查当前进程/线程是否是主进程/线程的正确方法是什么?

python - 散点图颜色条 - Matplotlib

python - PyQt5:创建具有非透明子项的半透明窗口

Python crypt 包 : Can type extra characters in password

r - 为绘图添加额外的图例