使用Hist函数在Python 3d plot中构建一系列1D直方图

use*_*413 3 python matplotlib

我使用pythons hist函数生成1d直方图,每个直方图链接到给定的实验.现在我明白了Hist功能允许人们在同一个x轴上绘制多个直方图进行比较.为了这个目的,我通常使用类似于下面的东西,结果是一个非常好的情节,其中x1,x2和x3定义如下x1 =长度expt1 x2 =长度expt2 x3 =长度expt3

P.figure()
n, bins, patches = P.hist( [x0,x1,x2], 10, weights=[w0, w1, w2], histtype='bar')
P.show()
Run Code Online (Sandbox Code Playgroud)

我希望尝试实现3d效果,因此我要求剂量任何人都知道是否有可能让每个独特的直方图在y平面中由给定单位偏移,从而产生3d效果.

我将不胜感激任何帮助.

Joe*_*ton 5

我相信你只是想要matplotlib.pyplot.bar3d.

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

x0, x1, x2 = [np.random.normal(loc=loc, size=100) for loc in [1, 2, 3]]

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

yspacing = 1
for i, measurement in enumerate([x0, x1, x2]):
    hist, bin_edges = np.histogram(measurement, bins=10)
    dx = np.diff(bin_edges)
    dy = np.ones_like(hist)
    y = i * (1 + yspacing) * np.ones_like(hist)
    z = np.zeros_like(hist)
    ax.bar3d(bin_edges[:-1], y, z, dx, dy, hist, color='b', 
            zsort='average', alpha=0.5)

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述