【发布时间】:2015-05-20 08:44:02
【问题描述】:
我尝试使用以下代码创建与 seaborn 的联合图:
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
testdata = pd.DataFrame(np.array([[100, 1, 3], [5, 2, 6], [25, 3, -4]]), index=['A', 'B', 'C'], columns=['counts', 'X', 'Y'])
counts = testdata['counts'].values
sns.jointplot('X', 'Y', data=testdata, kind='kde', joint_kws={'weights':counts})
plt.savefig('test.png')
现在joint_kws 不会引发错误,但从图中可以看出,权重肯定没有被考虑在内:
我也尝试使用JointGrid 来实现,将权重传递给边缘分布:
g = sns.JointGrid('X', 'Y', data=testdata)
x = testdata['X'].values
y = testdata['Y'].values
g.ax_marg_x.hist(x, bins=np.arange(-10,10), weights=counts)
g.ax_marg_y.hist(y, bins=np.arange(-10,10), weights=counts, orientation='horizontal')
g.plot_marginals(sns.distplot)
g.plot_join(sns.kdeplot, joint_kws={'weights':counts})
plt.savefig('test.png')
但这仅适用于边缘分布,而联合图仍然没有加权:
有人知道怎么做吗?
【问题讨论】:
-
好吧,我可能不适合这里,但你到底想看什么?
-
抱歉不清楚。我想加权数据点。 A、B 和 C 的权重分别为 100、5 和 25,因此数据点“A”应该比“B”更重要,并且对分布的贡献更大。与上图中的边缘分布相比,下图中的边缘分布显示了这种加权分布。
-
这是一种不用 seaborn 的方法:gist.github.com/tillahoffmann/…