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?
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With