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

标签 python numpy matplotlib

编辑 - 重新设计的问题

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

每一代的频率数据包含在一个形状的 numpy 数组中(# Generations,#bin_edges),通过在每一代上运行 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, ...]

我已经进行了多次尝试,但似乎无法将直方图数据转换为正确的格式,或者可能无法将直方图数据转换为 matplotlibaxes.bar 的形状。

尝试 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()

尝试 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()

最佳答案

我使用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()

关于python - (hist, bin_edges) 列表的 3d 图,其中直方图条形图或线条位于 z-y 平面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48038422/

相关文章:

python - plt.show() 使终端挂起

python - 就速度和内存而言,迭代非常大的循环并将 scipy 稀疏矩阵存储到文件中的最有效方法

python - 有没有更快的方法来获取 MNIST 数据集的 Local Binary Pattern?

python - 替换 Numpy 图像中的像素值

python - 如何使用 NumPy 生成相邻索引

python - 保存 Matplotlib 动画

python - 在 python 中更改绘图标签的大小

python - 在Python中以最后一个元素为基准实现快速排序

python - 从图像(OCR)中读取具有不同行颜色的数字表

python - Azure 事件中心 - 一条一条地消费消息