How to remove a JointGrid margin frame

103 Views Asked by At
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# sample data
penguins = sns.load_dataset('penguins')

g = sns.JointGrid(data=penguins, x='bill_length_mm', y='bill_depth_mm', space=0) 

g.plot_joint(sns.scatterplot)
sns.despine(top=False)
g.plot_marginals(sns.histplot, kde=True, bins=250, color='r')
g.ax_marg_x.remove()

In this code, I am trying to plot a figure and marginal distribution of an axis. However, I could not do something. How can I remove the right and left margin borders in this figure?

enter image description here

2

There are 2 best solutions below

0
On
  • Instead of using g.plot_marginals, which plots the function in both margins, and then requires several lines of code to reformat, only plot in the specific margin, ax=g.ax_marg_y.
g = sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm", space=0) 

g.plot_joint(sns.scatterplot)

sns.histplot(data=penguins, y="bill_depth_mm", kde=True, bins=250, color='r', ax=g.ax_marg_y)

enter image description here

  • The other option is to use sns.jointplot
g = sns.jointplot(data=penguins, x='bill_length_mm', y='bill_depth_mm', space=0,
                  marginal_kws=dict(bins=250, fill=False, kde=True, color='r'))

# remove the x-margin plot, which includes the spine for the margin
g.ax_marg_x.remove()

# makes the joint spine visible
g.ax_joint.spines[['top']].set_visible(True)
0
On

set_frame_on(False) works with g.ax_marg_y, and g.ax_marg_x, to remove the y margin frame, and x margin frame, respectively.

g = sns.JointGrid(data=penguins, x='bill_length_mm', y='bill_depth_mm', space=0) 

g.plot_joint(sns.scatterplot)
sns.despine(top=False, right=False)
g.plot_marginals(sns.histplot, kde=True, bins=250, color='r')
g.ax_marg_x.remove()

g.ax_marg_y.set_frame_on(False)

enter image description here