渲染具有不同的预先计算的 bin 形状和频率的直方图

Render a histogram with varying pre-calculated bin shapes and frequencies

我在 [a,b] 形式上有一个 bin 形状和位置的规范,其中 a 和 b 是两个大小为 2 的 numpy 数组(用于维度)。

对于每个箱子,我还有箱子中数据的频率。规范可以是这样的:

import numpy as np
bins = np.array([[[0,0],[1/2,1/2]],[[1/2,0],[1,1/2]],[[0,1/2],[1/2,1]],[[1/2,1/2],[4/5,4/5]],[[4/5,1/2],[1,4/5]],[[1/2,4/5],[4/5,1]],[[4/5,4/5],[1,1]]])
weights = [10,20,5,35,10,15,5]

我如何绘制一个看起来像这个 但带有多个分箱的直方图?或者类似于 sns.heatmap 的输出(即在 2 维中),但具有指定的不规则网格。

我不确定你的 x-y 约定,但这应该适合你:

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

fig = plt.figure(figsize=(6, 5))
ax1 = fig.add_subplot(111, projection='3d')

bins = np.array([[[0,0],[1/2,1/2]],[[1/2,0],[1,1/2]],[[0,1/2],[1/2,1]],[[1/2,1/2],[4/5,4/5]],[[4/5,1/2],[1,4/5]],[[1/2,4/5],[4/5,1]],[[4/5,4/5],[1,1]]])
weights = np.array([10,20,5,35,10,15,5])
x = bins[:,0,0]
dx = bins[:,1,0]
y = bins[:,0,1]
dy = bins[:,1,1]

bottom = np.zeros_like(weights)

cmap = cm.get_cmap('viridis')
rgba = [cmap((k-np.min(weights))/np.max(weights)) for k in weights] 
ax1.bar3d(x, y, bottom, dx, dy, weights, shade=True, zsort='max', color=rgba)
plt.show()