Seaborn: Re-use axis on a sns.jointplot() ?

Created on 17 Sep 2014  路  4Comments  路  Source: mwaskom/seaborn

Hi,

I'm trying to plot something like this example. I'd like to add two other distributions to the same plot. But I don't think I can since jointplot is a figure-level function. It would be nice to able to, though. Keep up the great work with Seaborn!

Cheers,

Caroline

question

Most helpful comment

I think in your case I'd do something like this using the tips dataset as an example:

tips = sns.load_dataset("tips")
g = sns.JointGrid("total_bill", "tip", tips)
for day, day_tips in tips.groupby("day"):
    sns.kdeplot(day_tips["total_bill"], ax=g.ax_marg_x, legend=False)
    sns.kdeplot(day_tips["tip"], ax=g.ax_marg_y, vertical=True, legend=False)
    g.ax_joint.plot(day_tips["total_bill"], day_tips["tip"], "o", ms=5)

Requires a little more work on your end than just using jointplot, but a lot less than building from scratch with pure matplotlib.

All 4 comments

Ah, you definitely can add stuff to plots made with figure-level functions, you just have to do so after calling them. In the case of jointplot, it returns an object of class JointGrid that exposes the three component axes at attributes called ax_joint, ax_marg_x, and ax_marg_y. So you can add you additional plots on those objects. Make sense?

Right. I can add a plot, but it doesn't generate all the bells and whistles that jointplot does. I want to get something like the figure 3 in this paper. I was going to worry about the contour aspect of the plot afterwards.

Thanks for the quick reply! I closed this by mistake. I'm sorry.

I think in your case I'd do something like this using the tips dataset as an example:

tips = sns.load_dataset("tips")
g = sns.JointGrid("total_bill", "tip", tips)
for day, day_tips in tips.groupby("day"):
    sns.kdeplot(day_tips["total_bill"], ax=g.ax_marg_x, legend=False)
    sns.kdeplot(day_tips["tip"], ax=g.ax_marg_y, vertical=True, legend=False)
    g.ax_joint.plot(day_tips["total_bill"], day_tips["tip"], "o", ms=5)

Requires a little more work on your end than just using jointplot, but a lot less than building from scratch with pure matplotlib.

Perfect! It works great!!! I'll send you an e-mail with my plot. Thank you so much for the quick responses! I'm at a conference with Fernando Perez. He said he'll see you soon and I asked him to thank you in person. ;)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stonebig picture stonebig  路  4Comments

amelio-vazquez-reina picture amelio-vazquez-reina  路  3Comments

JanHomann picture JanHomann  路  3Comments

rrbarbosa picture rrbarbosa  路  3Comments

chanshing picture chanshing  路  3Comments