"TypeError: Image data of dtype object cannot be converted to float" when drawing 2d histogram using matplotlib.pyplot.imshow()

"TypeError: Image data of dtype object cannot be converted to float" when drawing 2d histogram using matplotlib.pyplot.imshow()

我使用 matplotlib.pyplotlt.hist2d() 制作了二维直方图,但由于函数 hist2d() 本身没有插值(平滑)选项,我尝试使用 matplotlib.pyplot.imshow().

以下是我的部分代码:

fig, ax=plt.subplots(figsize=(8,6))
ax.set_title('Joint Distribution of NND for San Ramon Earthquakes')
h=ax.hist2d(T,D,30,density=True)
ax.imshow(h, interpolation='nearest')
plt.colorbar(h[3],ax=ax)

这给出了错误 TypeError: Image data of dtype object cannot be converted to float. 我搜索了类似的示例,但我无法弄清楚我的代码有什么问题。

我该如何解决这个问题? 或者是否有不使用 imshow() 的二维直方图插值的替代方法?

我在没有插值的情况下生成的图如下:

hist2d matplotlib 函数 returns binned 二维数组,还有 xedgesyedges,检查 docs here。您可能想尝试:

ax.imshow(h[0], interpolation='nearest')