Python 直方图条形图或直线位于z-y平面上的(历史、bin_边)列表的3d图
编辑-返工问题 我需要打印50代计算机程序的健身数据的3D柱状图。该数据采用DEAP框架计算并存储在日志中。曲线图的形式需要在z轴上具有适应度频率,在x轴上具有生成,在y轴上具有bin_边。因此,直方图的线在z-y平面上,对于x轴上的每一代 每一代的频率数据包含在一个形状的numpy数组中(#代,#bin_边),通过在每一代上运行np.histogram()获得Python 直方图条形图或直线位于z-y平面上的(历史、bin_边)列表的3d图,python,numpy,matplotlib,Python,Numpy,Matplotlib,编辑-返工问题 我需要打印50代计算机程序的健身数据的3D柱状图。该数据采用DEAP框架计算并存储在日志中。曲线图的形式需要在z轴上具有适应度频率,在x轴上具有生成,在y轴上具有bin_边。因此,直方图的线在z-y平面上,对于x轴上的每一代 每一代的频率数据包含在一个形状的numpy数组中(#代,#bin_边),通过在每一代上运行np.histogram()获得 histograms = [el[0] for el in logbook.chapters["fitness"].select("h
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 Axis.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()
我使用
mplot3d
和bar
绘制3d历史,如下所示:
你尝试了什么?我正在尝试通过mplot3 hist3d演示解决一个类似的问题谢谢@消音器,我已经尝试运行了,但是导入cv2时出错,未找到符号:\u clock\u gettime
。我认为问题在于直方图数据的结构,因为它在z-y平面,对于x中的每个箱子。我看到的所有示例都在z-x平面上有线条。@BlueShrapnel我的帖子链接中需要OpenCV,但你的链接不需要OpenCV。你没有给出你的直方图数据格式和内容。很难说为什么。谢谢你!这绝对是一种我还不了解的黑色艺术。我如何才能更改垃圾箱轴的标签?我已经尝试了ax.set_xticklabels(bin_标签[1:])
其中bin_标签是['0','9','18','27','36','45','54','63','72','81','90']
。但是没有变化。修改这一行:xs=xxx
根据需要,注意尺寸。如何更改角度?
#!/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()