Home > Blockchain >  Multiple marginal plots with Seaborn jointgrid plot
Multiple marginal plots with Seaborn jointgrid plot

Time:08-04

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()

CodePudding user response:

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

  • Related