为什么 hist2d 绘制统一密度图?

问题描述

我正在绘制 ~40k 点的密度图,但 hist2d 返回一个均匀的密度图。这是我的代码

hist2d(x,y,bins=(1000,1000),cmap=plt.cm.jet)

这是散点图

enter image description here

这是直方图

enter image description here

我期待在中心有一个红色的水平部分,逐渐变成蓝色,朝向更高/更低的 y 值

编辑: @bb1 建议减少 bin 数量,但通过将其设置为 bins=(100,1000),我得到了这个结果

enter image description here

解决方法

我认为您指定的 bin 过多。通过设置 bins=(1000,000),您可以获得 1,000,000 个 bin。有 40,000 个点时,大部分 bin 将是空的,它们会淹没图像。

您也可以考虑使用 seaborn kdeplot() 函数而不是 plt.hist2d()。它将可视化数据密度,而无需将数据细分为 bin:

import seaborn as sns
sns.kdeplot(x=x,y=y,levels = 100,fill=True,cmap="mako",thresh=0)