python - seaborn jointplot 按密度颜色

标签 python matplotlib seaborn histogram2d

我正在为一些具有数百万个数据点的数据制作二维直方图。 matplotlib.hist2d(x,y,bins,norm=LogNorm()) 运行良好并在大约 5 秒内生成绘图,但我喜欢 seaborn.jointplot()< 的边际直方图。如何使用随附的 matplotlib.hist2d() 图中点的对数密度为 seaborn.jointplot() 中的点着色?使用 KDE 花费的时间太长(大约一分钟后我就放弃了),而且我有很多图形要创建。所以“获得”颜色的时间是一个因素。或者,如何将边际直方图添加到 matplotlib.hist2d()

plt.hist2d(x,y,100,norm=LogNorm(),cmap='jet')

enter image description here

sns.jointplot(x=x, y=y)

enter image description here

最佳答案

seaborn 中可能还有另一种直接获取颜色图的方法。我还没有找到。这是一个 hacky 示例解决方案,用于使用一些随机数据完成工作。至于你的第二个问题,我建议发布一个新问题。

诀窍是首先使用 seaborn 创建一个 jointplot,然后隐藏 2d 散点图并使用 plt.hist2d 重新绘制它

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

# some random data
x = np.random.normal(size=100000)
y = x * 3.5 + np.random.normal(size=100000)

ax1 = sns.jointplot(x=x, y=y)
ax1.ax_joint.cla()
plt.sca(ax1.ax_joint)

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

enter image description here

关于python - seaborn jointplot 按密度颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53964485/

相关文章:

python - Seaborn 折线图样式导致重复的图例条目

python - 为什么会给我TypeError:参数 'src'预期为cv::UMat?

python - 用不同的颜色为一条线设置动画

python - 无法将 pandas Series 传递给 pyplot 的 fill_ Between 函数?

python - Seaborn tsplot中的 "error bands"是怎么计算出来的?

python - 你如何在 Seaborn 中为 kde plot 创建一个传奇?

python - 获取 DataFrame 中按组的日期计数

python - 是否可以使用 sympy 符号索引 numpy 数组?

python - 选择多于 C 列中值大于 V 的行

python - 在 Plotly 中更改/更新 xtick 标签和 ytick 标签不一致