我嘗試使用下面的代碼來創建一個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不會引發錯誤,但權重肯定不會考慮爲在曲線中可以看出:
我也試圖與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'重要得多,並且對分佈貢獻更多。下圖中的邊際分佈與上圖中的邊際分佈相比顯示了這種加權分佈。 – madcap
下面是一個沒有seaborn的方法:https://gist.github.com/tillahoffmann/f844bce2ec264c1c8cb5#file-weighted_kde-ipynb – Dan