4

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?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
tim__25
  • 41
  • 1
  • 5
  • I don't think I understand...Isn't the second code sample the answer to your question? – mwaskom Jul 01 '15 at 20:12
  • Thx, mwaskom, for your response. Yes that would be the answer, but unfortunately, these methods don't exist or don't work that way... – tim__25 Jul 02 '15 at 07:22
  • `ax_marg_x` and `ax_marg_y` are just the actual matplotlib Axes objects, so you could call any method on them (e.g. `ax.hist()`) or pass them to the `ax=` parameter of a seaborn function like `distplot`. – mwaskom Jul 02 '15 at 14:49
  • Thanks again for your reply. But unfortunately either it doesn't work or i didn't get what u tried me to do. – tim__25 Jul 02 '15 at 20:02
  • Thanks again for your reply. But unfortunately either it doesn't work or i didn't get what u tried me to do. `myPlot = sns.JointGrid(x="volume_x", y="volume_y", data=c); myPlot.plot_joint(plt.scatter); myPlot.ax_marg_x = plt.hist(c["volume_x"]); myPlot.ax_marg_y = plt.hist(c["volume_y"], orientation="horizontal")` all the single plots work fine if I just print them for themself, but if I add the histograms directly to the marginal axes using `=` nothing is shown on the grid. Can you perhaps write out the lines you wanted me to test? Thanks again for your great help! – tim__25 Jul 02 '15 at 20:08

1 Answers1

1

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)
michael_j_ward
  • 4,369
  • 1
  • 24
  • 25