gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:19:23 24 4
gpt4 key购买 nike

编辑 - 重新设计的问题

我需要打印 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com