BozhiWang
BozhiWang

Reputation: 113

In jointplot when using seaborn, how to set another legend in figure

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

enter image description here

  1. Where can I find all the detailed keywords that control the size, font, location of legend and other things I can custom the fig by my own. Because I did not find some very detailed description about keywords in seaborn's document in their official website. Where can I get that? Thanks

Upvotes: 2

Views: 2919

Answers (1)

Diziet Asahi
Diziet Asahi

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)

enter image description here

Upvotes: 3

Related Questions