Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

seaborn

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

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

Now the joint_kws doesn't raise an error, but the weights sure are not taken into account as can be seen in the plot:

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:

Has anyone an idea how to do this?

like image 785
madcap Avatar asked May 20 '15 08:05

madcap


Video Answer


1 Answers

Unfortunately this seems to be impossible.

The was a feature request filed in December 2015, but is was closed as will-not-fix.

It is also discussed in this StackOverflow question: weights option for seaborn distplot?

like image 200
Tobber Avatar answered Nov 15 '22 00:11

Tobber