question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

jointplot plot_marginals boxplot

See original GitHub issue

Is it possible to generate a jointplot with boxplots as marginals instead of a distplot?

From the arguments it obviously does not seem possible and the error is throwing when I force it suggests that as well:

g.plot_marginals(sns.boxplot) > TypeError: boxplot() got an unexpected keyword argument 'vertical'

Is this a feasible feature request? Or is there any alternative within seaborn to achive something similar?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
EmanuelGoncalvescommented, Jun 1, 2017

I solved my problem by hacking the marginal_boxplot function and defining there the data.frame and the variables to plot.

It’s definitely not portable to other situations but in case it is useful for anyone else I leave it here the code and the outcome plot.

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

plot_df = pd.read_csv('jointplot_boxplot_marginal.csv')

pal = {1: '#eb2525', 0: '#888888'}


def marginal_boxplot(a, vertical=False, **kws):
    g = sns.boxplot(x='var_z', y='var_y', orient='v', **kws) if vertical else sns.boxplot(x='var_x', y='var_z', orient='h', **kws)
    g.set_ylabel('')
    g.set_xlabel('')

sns.set(rc={'axes.linewidth': .3, 'xtick.major.width': .3, 'ytick.major.width': .3, 'xtick.major.size': 2.5, 'ytick.major.size': 2.5}, style='ticks', context='paper', font_scale=.75)

g = sns.jointplot(
    'var_x', 'var_y', plot_df, 'reg', color=pal[0], space=0, truncate=True, ratio=8,
    marginal_kws={
        'hist': False, 'rug': False, 'kde': False,
    },
    annot_kws={
        'template': 'Pearson={val:.2g}, p={p:.1e}', 'loc': 4
    },
    joint_kws={
        'scatter_kws': {'s': 20, 'edgecolor': 'w', 'linewidth': .5, 'alpha': .8},
        'line_kws': {'linewidth': .5}
    }
)

g.plot_marginals(marginal_boxplot, palette=pal, data=plot_df, linewidth=.3, fliersize=1, notch=True)

g.x = plot_df.loc[plot_df['var_z'] == 1, 'var_x']
g.y = plot_df.loc[plot_df['var_z'] == 1, 'var_y']
g.plot_joint(sns.regplot, color=pal[1], truncate=True, fit_reg=False, scatter_kws={'s': 20, 'edgecolor': 'w', 'linewidth': .5, 'alpha': .8})

g.ax_joint.axhline(0, ls='-', lw=0.3, c='black', alpha=.2)
g.ax_joint.axvline(0, ls='-', lw=0.3, c='black', alpha=.2)

plt.gcf().set_size_inches(2, 2)
plt.savefig('jointplot_boxplot_marginal.png', bbox_inches='tight', dpi=600)
plt.close('all')

jointplot_boxplot_marginal

1reaction
mwaskomcommented, Jun 1, 2017

But I have the impression you were suggesting something different.

Yes, I’m saying that once the JointGrid is created, you don’t have to use its methods to plot things on it. It has an attribute called ax_joint that is simply the underlying matplotlib axes and can be passed to any axes-level seaborn function (or any of its plotting methods can be called if you want to use matplotlib directly).

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - How to overlay a Seaborn jointplot with a "marginal ...
I have plotted a Seaborn JointPlot from a set of "observed counts vs concentration" which are stored in a pandas DataFrame .
Read more >
seaborn.jointplot — seaborn 0.12.1 documentation - PyData |
An object managing multiple subplots that correspond to joint and marginal axes for plotting a bivariate relationship or distribution. See also. JointGrid. Set ......
Read more >
Understanding the jointplot vs jointgrid and the seaborn ...
Learn about the seaborn JointGrid in this Python seaborn tutorial video. I begin by explaining what is the joint grid and how it...
Read more >
Marginal distribution plots in Python
Marginal distribution plots are small subplots above or to the right of a main plot, which show the distribution of data along only...
Read more >
Marginal Plot with Seaborn - Python Graph Gallery
This post explains how to draw a marginal plot using jointplot() function of seaborn. Several examples are given using scatterplot, hexbin and density...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found