【发布时间】:2012-01-16 06:58:47
【问题描述】:
这是一个关于在 python 中创建 3d 直方图的一般性问题。
我尝试在以下代码中使用 X 和 Y 数组创建 3d 直方图
import matplotlib
import pylab
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D
from matplotlib import cm
def threedhist():
X = [1, 3, 5, 8, 6, 7, 1, 2, 4, 5]
Y = [3, 4, 3, 6, 5, 3, 1, 2, 3, 8]
fig = pylab.figure()
ax = Axes3D(fig)
ax.hist([X, Y], bins=10, range=[[0, 10], [0, 10]])
plt.xlabel('X')
plt.ylabel('Y')
plt.zlabel('Frequency')
plt.title('Histogram')
plt.show()
但是,我收到以下错误
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
a3dhistogram()
File "C:/Users/ckiser/Desktop/Projects/Tom/Python Files/threedhistogram.py", line 24, in a3dhistogram
ax.hist([X, Y], bins=10, range=[[0, 10], [0, 10]])
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 7668, in hist
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
File "C:\Python27\lib\site-packages\numpy\lib\function_base.py", line 169, in histogram
mn, mx = [mi+0.0 for mi in range]
TypeError: can only concatenate list (not "float") to list
我已经尝试过在行中有和没有“[”的代码 ax.hist([X, Y], bins=10, range=[[0, 10], [0, 10]]) 我也尝试过 numpy 的功能但没有成功 H, xedges, yedges = np.histogram2d(x, y, bins = (10, 10)) 我是否缺少步骤或参数?任何建议将不胜感激。
【问题讨论】:
-
你检查下面的答案了吗?
标签: python numpy matplotlib 3d histogram