2015-05-20 27 views
4

我嘗試使用下面的代碼來創建一個jointplot與seaborn:如何權重參數傳遞給seaborn的jointplot()(或底層kdeplot)

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不會引發錯誤,但權重肯定不會考慮爲在曲線中可以看出: enter image description here

我也試圖與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') 

但這僅適用於邊際分佈,而聯合策劃仍未加權:

enter image description here

有沒有人一個想法如何做到這一點?

+0

好吧,我可能會在這裏我的元素,但你想到底該怎麼看? –

+0

對不起,不清楚。我想衡量數據點。對於A,B和C,權重分別爲100,5和25,所以數據點'A'應該比'B'重要得多,並且對分佈貢獻更多。下圖中的邊際分佈與上圖中的邊際分佈相比顯示了這種加權分佈。 – madcap

+0

下面是一個沒有seaborn的方法:https://gist.github.com/tillahoffmann/f844bce2ec264c1c8cb5#file-weighted_kde-ipynb – Dan

回答

-1

你真的很接近。

保持的事情是,加入的情節做了以下(重意譯):

def jointplot(x, y, data=None, ..., joint_kws): 
    g = sns.JointGrid(...) 
    g.plot_joint(..., **joint_kws) 

所以,當你調用g.plot_joint自己,只給它正常kwargs:

import numpy as np 
import matplotlib.pyplot as plt 
import pandas as pd 
import seaborn as sns 

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 

g = sns.JointGrid('X', 'Y', data=testdata) 
g.plot_marginals(sns.distplot) 
g.plot_joint(sns.kdeplot, weights=counts) 

enter image description here

現在我不確定這看起來是否正確,但它沒有倒酒,所以這是值得的。

+5

這聽起來很合理,但情節仍然沒有加權。取點A(x = 1,y = 3)。它的數量是100.所有計數的總和是130(100 + 5 + 25)。因此,A應該是100/130或0.77,整個分配肯定會有最大的峯值。 – madcap

相關問題