用不等长数组绘制三维直方图

2021-05-16 08:20:19 发布

您现在位置:Python中文网/ 问答频道 /正文

这是示例代码

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

a = floor(100*random(100)) # create 100 random point
b = floor(100*random(75))
c = floor(100*random(68))
  :
  :
n = floor(100*random(45))

data = [a, b, c, ..., n]

现在,我想在制作

^{pr2}$

它要么显示条形图,要么显示三维曲面。你的建议将得到认可。在

1条回答
网友
1楼 ·

可能使用ax.bar3d

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d
import matplotlib.cm as cm

np.random.seed(3)

a = np.random.random_integers(100, size = (100, ))
b = np.random.random_integers(100, size = (75, ))
c = np.random.random_integers(100, size = (68, ))
n = np.random.random_integers(100, size = (45, ))
data = (a,b,c,n)
# data = np.random.random_integers(100, size = (4, 100))  # also possible
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection = '3d')

for i, arr in enumerate(data):
    hist, bin_edges = np.histogram(arr, bins = 10)
    x = bin_edges[:-1]
    y = i*np.ones_like(hist)
    z = np.zeros_like(hist)
    dx = np.diff(bin_edges)
    dy = 0.01
    dz = hist
    color = cm.RdBu(float(i)/len(data))
    ax.bar3d(x, y, z, dx, dy, dz, color = color, alpha = 0.5)

plt.show()

enter image description here

相关问题