Reputation: 113
1.I want to plot a joint distribution figure in 'hex' style with Seaborn, and I want to add a regression fitting using another data "all_data", and I want the Pearson 'r' and 'p-value' is calculated by 'all_data', but now it's calculated by 'data' which is used to draw 'hex' plot. How can I change the code to achieve my goal. What keywords should I add?
joint_kws={'gridsize':30}
g=sns.jointplot('nature','space',data,kind='hex',size=20,joint_kws= joint_kws)
sns.regplot(x='nature', y='space',data=all_data, ax=g.ax_joint, scatter=False,color='red')
Upvotes: 2
Views: 2919
Reputation: 40697
You will not be able to get the statistical info from the regplot()
directly. The author of seaborn has said this was not something he wanted to add.
Instead, you will have to calculate the r-value yourself (i.e. using statsmodel
or scipy
), and write directly in the legend.
Concerning your second question, you can adjust the appearance of the legend generated by jointplot()
using the annot_kws=
parameter. This is a dictionnary of values that are passed to legend()
upon legend creation. Therefore, to know the valid options, refer to the documentation for legend()
.
tips = sns.load_dataset("tips")
annot_kws = {'prop':{'family':'monospace', 'weight':'bold', 'size':15}}
# you change the properties of the legend directly when calling jointplot
g = sns.jointplot("total_bill", "tip", data=tips, kind="hex", annot_kws=annot_kws)
sns.regplot(x="total_bill", y="tip", data=tips, ax=g.ax_joint, scatter=False, color='red')
# calculate r and p-value here
r = 0.9
p = 0.05
# if you choose to write your own legend, then you should adjust the properties then
phantom, = g.ax_joint.plot([], [], linestyle="", alpha=0)
g.ax_joint.legend([phantom],['r={:f}, p={:f}'.format(r,p)], **annot_kws)
Upvotes: 3