BML*_*BML 4 python matplotlib histogram2d seaborn
我正在为具有数百万个数据点的一些数据制作二维直方图。matplotlib.hist2d(x,y,bins,norm=LogNorm())
效果很好,并在大约 5 秒内生成一个图,但我喜欢seaborn.jointplot()
. 如何seaborn.jointplot()
使用matplotlib.hist2d()
附图中的点的对数密度为点着色?使用 KDE 花费的时间太长(大约一分钟后我放弃了),而且我有很多数字要创建。所以“获得”颜色的时间是一个因素。或者,如何将边际直方图添加到matplotlib.hist2d()
?
plt.hist2d(x,y,100,norm=LogNorm(),cmap='jet')
sns.jointplot(x=x, y=y)
可能还有另一种直接的方法可以在seaborn
. 我还没有找到。这是一个使用一些随机数据完成工作的hacky示例解决方案。至于你的第二个问题,我建议发布一个新问题。
诀窍是首先jointplot
使用 seaborn创建一个,然后隐藏 2d-scatter 并使用重新绘制它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);
Run Code Online (Sandbox Code Playgroud)
这是另一种类似的方法,但坚持在seaborn中:
import seaborn as sns
import numpy as np
x = np.random.normal(size=100)
y = x * 3.5 + np.random.normal(size=100)
sns.jointplot(x=x, y=y, kind='kde', cmap='hot_r', n_levels=60, fill=True)
Run Code Online (Sandbox Code Playgroud)