如何移除 JointGrid 的边框和边距

huangapple go评论66阅读模式
英文:

How to remove a JointGrid margin frame

问题

在这段代码中,我试图绘制一个图形以及一个轴的边际分布。然而,我无法做到某些事情。如何在这个图形中移除右侧和左侧的边框?

如何移除 JointGrid 的边框和边距

英文:
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?

如何移除 JointGrid 的边框和边距

答案1

得分: 1

set_frame_on(False)适用于g.ax_marg_yg.ax_marg_x,分别用于移除y边缘框和x边缘框。

英文:

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)

如何移除 JointGrid 的边框和边距

答案2

得分: 1

  • 而不是使用 g.plot_marginals,该函数在两个边缘都绘制函数,然后需要多行代码来重新格式化,只在特定的边缘上绘制,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)
  • 另一个选项是使用 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'))

# 删除x边缘绘图,其中包括边缘的脊柱
g.ax_marg_x.remove()

# 使联合脊柱可见
g.ax_joint.spines[['top']].set_visible(True)

如何移除 JointGrid 的边框和边距

英文:
  • 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)

如何移除 JointGrid 的边框和边距

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

huangapple
  • 本文由 发表于 2023年5月23日 00:22:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定