(hist,bin_edges)列表的3d图,其中直方图条形图或线在z-y平面中

时间:2017-12-30 22:30:20

标签: python numpy matplotlib

编辑 - 重写问题

我需要为50代计算机程序打印健身数据的3D直方图。使用DEAP框架计算此数据并将其存储在日志中。绘图的形式需要与z轴上的适合度频率,x轴上的生成和y轴上的bin_edges有关。因此,对于x轴上的每一代,直方图的线都在z-y平面中。

每一代的频率数据都包含在一个形状(#generations,#bin_edges)的numpy数组中,通过在每一代上运行np.histogram()获得。

histograms = [el[0] for el in logbook.chapters["fitness"].select("hist")]

histograms.shape
(51, 10)  # (num gen, num bins)

print (histograms)  # excerpt only
[[ 826.  145.   26.    2.    1.    0.    0.    0.    0.    0.]
 [ 389.  446.  145.   16.    4.    0.    0.    0.    0.    0.]
 [ 227.  320.  368.   73.   12.    0.    0.    0.    0.    0.]
 [ 199.  128.  369.  261.   43.    0.    0.    0.    0.    0.]
 [ 219.   92.  158.  393.  137.    1.    0.    0.    0.    0.]
 [ 252.   90.   91.  237.  323.    6.    1.    0.    0.    0.]
 [ 235.   89.   69.   96.  470.   36.    5.    0.    0.    0.]
 [ 242.   78.   61.   51.  438.  114.   16.    0.    0.    0.]
 [ 235.   82.   52.   52.  243.  279.   57.    0.    0.    0.]]

bin_edges
array([  0.,   9.,  18.,  27.,  36.,  45.,  54.,  63.,  72.,  81.,  90.])

gen
[0, 1, 2, 3, 4, 5, 6, 7, 8, ...]

我做了几次尝试,但似乎无法将直方图数据转换成正确的格式,或者可能是matplotlib axes.bar的形状。

ATTEMPT 2:忙于重做

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
xedges = list(gen)
yedges = list(bin_edges)
H = histograms.T

fig=plt.figure()
# error on this line: TypeError: list indices must be integers or slices, not list
ax = fig.add_subplot(133, title='NonUniformImage: interpolated', aspect='equal', \

xlim=xedges[[0, -1]], ylim=yedges[[0, -1]])
im = mpl.image.NonUniformImage(ax, interpolation='bilinear')
xcenters = (xedges[:-1] + xedges[1:]) / 2
ycenters = (yedges[:-1] + yedges[1:]) / 2
im.set_data(xcenters, ycenters, H)
ax.images.append(im)
plt.show()

ATTEMPT 1:

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# I have a list of len(gen) histograms
# with an array of freq count for each bin

xs =  list(gen)
ys = list(bin_edges)
zs = hist.T #ndarray


# error occurs here as 
# ValueError: shape mismatch: objects cannot be broadcast to a single shape    
ax.bar(xs, ys, zs)
plt.show()

1 个答案:

答案 0 :(得分:0)

我使用mplot3dbar来制作3d-hist,如下所示:

enter image description here

#!/usr/bin/python3
# 2017.12.31 18:46:42 CST
# 2017.12.31 19:23:51 CST
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

## the hist data
data = np.array([
        np.array([826, 145,  26,   2,   1,   0,   0,   0,   0,   0]),
        np.array([389, 446, 145,  16,   4,   0,   0,   0,   0,   0]),
        np.array([227, 320, 368,  73,  12,   0,   0,   0,   0,   0]),
        np.array([199, 128, 369, 261,  43,   0,   0,   0,   0,   0]),
        np.array([219,  92, 158, 393, 137,   1,   0,   0,   0,   0]),
        np.array([252,  90,  91, 237, 323,   6,   1,   0,   0,   0]),
        np.array([235,  89,  69,  96, 470,  36,   5,   0,   0,   0]),
        np.array([242,  78,  61,  51, 438, 114,  16,   0,   0,   0]),
        np.array([235,  82,  52,  52, 243, 279,  57,   0,   0,   0])
        ])

## other data 
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
colors = ["r","g","b"]*10

## Draw 3D hist 
ncnt, nbins = data.shape[:2]
xs = np.arange(nbins)
for i in range(ncnt):
    ys = data[i]
    cs = [colors[i]] * nbins
    ax.bar(xs, ys.ravel(), zs=i, zdir='x', color=cs, alpha=0.8)

ax.set_xlabel('idx')
ax.set_ylabel('bins')
ax.set_zlabel('nums')
plt.show()