How to pass weights argument to seaborn’s jointplot() (or the underlying kdeplot)

I try to create a jointplot with seaborn by using the following code:

import seaborn as sns # and of course numpy, pyplot and pandas too
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')

Now the joint_kws don’t raise an error, but the weights sure are not taken into account as can be seen in the plot:
enter image description here

I also tried to do it with JointGrid, passing the weights to the marginal distributions:

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')

But this works only for the marginal distributions, while the joint plot still is not weighted:

enter image description here

Has anyone an idea how to do this?


Source: python

Leave a Reply