如何使用python中内置函数的numpy或matplotlib正确生成3d直方图?

Chr*_*ris 7 python 3d numpy matplotlib histogram

这更多是关于在 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()
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误

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
Run Code Online (Sandbox Code Playgroud)

我已经尝试过 ax.hist([X, Y], bins=10, range=[[0, 10], [0, 10]]) 行中带有和不带有“[”的代码我也尝试过来自 numpy 的函数没有成功 H, xedges, yedges = np.histogram2d(x, y, bins = (10, 10)) 我是否缺少步骤或参数?任何建议将不胜感激。

Art*_*exR 10

我在一个关于彩色 3d 条形图的相关线程中发布了这个,但我认为它在这里也很相关,因为我在任何一个线程中都找不到我需要的完整答案。此代码为任何类型的 xy 数据生成直方图散点图。高度表示该 bin 中值的频率。因此,例如,如果您有许多数据点 (x,y) = (20,20),则它会很高且呈红色。如果 bin 中的 (x,y) = (100,100) 中的数据点很少,则它会很低且呈蓝色。

注意:结果将有很大差异,具体取决于您拥有多少数据以及您为直方图选择的 bin 数量。相应调整!

xAmplitudes = #your data here
yAmplitudes = #your other data here

x = np.array(xAmplitudes)   #turn x,y data into numpy arrays
y = np.array(yAmplitudes)

fig = plt.figure()          #create a canvas, tell matplotlib it's 3d
ax = fig.add_subplot(111, projection='3d')

#make histogram stuff - set bins - I choose 20x20 because I have a lot of data
hist, xedges, yedges = np.histogram2d(x, y, bins=(20,20))
xpos, ypos = np.meshgrid(xedges[:-1]+xedges[1:], yedges[:-1]+yedges[1:])

xpos = xpos.flatten()/2.
ypos = ypos.flatten()/2.
zpos = np.zeros_like (xpos)

dx = xedges [1] - xedges [0]
dy = yedges [1] - yedges [0]
dz = hist.flatten()

cmap = cm.get_cmap('jet') # Get desired colormap - you can change this!
max_height = np.max(dz)   # get range of colorbars so we can normalize
min_height = np.min(dz)
# scale each z to [0,1], and get their rgb values
rgba = [cmap((k-min_height)/max_height) for k in dz] 

ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color=rgba, zsort='average')
plt.title("X vs. Y Amplitudes for ____ Data")
plt.xlabel("My X data source")
plt.ylabel("My Y data source")
plt.savefig("Your_title_goes_here")
plt.show()
Run Code Online (Sandbox Code Playgroud)

我的大约 75k 数据点的结果如下。请注意,您可以拖放到不同的视角,并且可能希望为演示文稿和后代保存多个视图。

3d 直方图侧视图 3d 直方图透视图 2


lxo*_*xop 3

看看 https://matplotlib.org/stable/gallery/mplot3d/hist3d.html,这有一个工作示例脚本。

我改进了该链接处的代码,使其更像是直方图:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = [1, 3, 5, 8, 6, 7, 1, 2, 4, 5]
y = [3, 4, 3, 6, 5, 3, 1, 2, 3, 8]

hist, xedges, yedges = np.histogram2d(x, y, bins=(4,4))
xpos, ypos = np.meshgrid(xedges[:-1]+xedges[1:], yedges[:-1]+yedges[1:])

xpos = xpos.flatten()/2.
ypos = ypos.flatten()/2.
zpos = np.zeros_like (xpos)

dx = xedges [1] - xedges [0]
dy = yedges [1] - yedges [0]
dz = hist.flatten()

ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b', zsort='average')
plt.xlabel ("X")
plt.ylabel ("Y")

plt.show()
Run Code Online (Sandbox Code Playgroud)

我不知道如何使用 Axes3D.hist () 来做到这一点。