tim__25
tim__25

Reputation: 41

How to add manually customised seaborn plots to JointGrid/jointplot

I want to use the seaborn JointGrid/jointplot.

In the middle I would like to add a sns.regplot. On the Margins two sns.distplots or histograms.

Thats not a problem, but I would like to custmize all three pplots differently and add different fiiting functions and labels to all three of them.

But I cant solve the problem, how to get the instances of the plots if i just use the predifined method:

g = sns.JointGrid(x="total_bill", y="tip", data=tips)
g = g.plot(sns.regplot, sns.distplot)

to manually manipulated those. Or the other way around to create one regplot and two distplot instances, which I define the way I wnat and then add them to the JointGrid, something in the meaning of, but unfortinaltely, these below methods don't exist or don't work that way:

g = sns.JointGrid(x="total_bill", y="tip", data=tips)
g.add_joint(myreg)           # That would be great but doesn't work
g.ax_marg_x.add(mydist1)     # Just invented code
g.ax_marg_y.add(mydist2)     # Just to show you, the way I'd like to solve the issue

Can you give me an advice, how to get around this issue?

Upvotes: 4

Views: 3695

Answers (1)

michael_j_ward
michael_j_ward

Reputation: 4559

Well, the documentation has a few examples. If you want the same distPlot on both marginals:

g = sns.JointGrid(x="total_bill", y="tip", data=tips)
g = g.plot_joint(plt.scatter, color=".5", edgecolor="white")
g = g.plot_marginals(sns.distplot, kde=True, color=".5")

Or if you want a different plot on each marginal

g = sns.JointGrid(x="total_bill", y="tip", data=tips)
g = g.plot_joint(sns.regplot, color="m")
_ = g.ax_marg_x.hist(tips["total_bill"], color="b", alpha=.6,
                     bins=np.arange(0, 60, 5))
_ = g.ax_marg_y.hist(tips["tip"], color="r", alpha=.6,
                     orientation="horizontal",
                     bins=np.arange(0, 12, 1))

And then it took 2 seconds of experimenting to make sure you could pass your own custom plot functions to both the marginals and jointPlot, and had to set a breakpoint to figure out what parameter determined the orientation of the marginal plot

def customJoint(x,y,*args,**kwargs):
    plt.scatter(x,y,color='red')
def customMarginal(x,*args,**kwargs):
    sns.distplot(x,color='green', vertical=kwargs['vertical'])

g = sns.JointGrid(x="total_bill", y="tip", data=tips)
g = g.plot(customJoint, customMarginal)

Upvotes: 1

Related Questions