英文:
How to remove a JointGrid margin frame
问题
在这段代码中,我试图绘制一个图形以及一个轴的边际分布。然而,我无法做到某些事情。如何在这个图形中移除右侧和左侧的边框?
英文:
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?
答案1
得分: 1
set_frame_on(False)
适用于g.ax_marg_y
和g.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)
答案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)
英文:
- 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)
- 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论