matplotlibplotseabornboxplotjointplot

Multiple marginal plots with Seaborn jointgrid plot


I'd like to draw a jointgrid plot with multiple marginal plots like below:

enter image description here

The reference code is:

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

penguins = sns.load_dataset("penguins")
print(penguins['species'])

plt.figure(figsize=(12,10))
g = sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species")

g.plot_joint(sns.scatterplot)
g.plot_marginals(sns.boxplot)

plt.show()

Solution

  • If you want stripplot plots on the marginal axes, you could just add the hue parameter:

    g = sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species")
    g.plot_joint(sns.scatterplot)
    g.plot_marginals(sns.stripplot, hue="species", dodge=True)
    

    But boxplot does not currently handle hue with only one coordinate variable assigned, so you need to draw each marginal plot separately:

    g = sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species")
    g.plot_joint(sns.scatterplot)
    sns.boxplot(penguins, x=g.hue, y=g.y, ax=g.ax_marg_y)
    sns.boxplot(penguins, y=g.hue, x=g.x, ax=g.ax_marg_x)
    

    enter image description here