【问题标题】:Plot 3D histogram with uneven length array绘制具有不均匀长度数组的 3D 直方图
【发布时间】:2012-01-17 20:47:41
【问题描述】:

这里是示例代码

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]

现在,我想在制作数据的同时绘制一个 3D 直方图

x-axis : value
y-axis : count w.r.t. to the value
z-axis : ith row of data metrix

显示条形或 3D 表面。您的建议将不胜感激。

【问题讨论】:

标签: python numpy matplotlib


【解决方案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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    相关资源
    最近更新 更多