这是一段代码片段
tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, col = 'time')
g = g.map(plt.hist, "tip")
Run Code Online (Sandbox Code Playgroud)
具有以下输出
我想在这些情节中引入despine偏移,同时保持其余不变.因此,我在现有代码中插入了despine函数:
tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, col = 'time')
g.despine(offset=10)
g = g.map(plt.hist, "tip")
Run Code Online (Sandbox Code Playgroud)
这导致以下图表
结果,偏移应用于轴.然而,ytick标签上右图回来了,这是我不想要的.
有人可以帮我吗?
要删除 yaxis 刻度标签,您可以使用以下代码:
库:
import seaborn as sns
sns.set_style('ticks')
Run Code Online (Sandbox Code Playgroud)
调整后的代码:
tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, col = 'time')
g.despine(offset=10)
g = g.map(plt.hist, "tip")
# IMPORTANT: I assume that you use colwrap=None in FacetGrid constructor
# loop over the non-left axes:
for ax in g.axes[:, 1:].flat:
# get the yticklabels from the axis and set visibility to False
for label in ax.get_yticklabels():
label.set_visible(False)
ax.yaxis.offsetText.set_visible(False)
Run Code Online (Sandbox Code Playgroud)
更一般地说,想象一下,您现在有一个 2x2 FacetGrid,您想要使用偏移量来进行调整,但 x- 和 yticklabels 返回:
使用以下代码将它们全部删除:
tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, col = 'time', row='sex')
g.despine(offset=10)
g = g.map(plt.hist, "tip")
# IMPORTANT: I assume that you use colwrap=None in FacetGrid constructor
# loop over the non-left axes:
for ax in g.axes[:, 1:].flat:
# get the yticklabels from the axis and set visibility to False
for label in ax.get_yticklabels():
label.set_visible(False)
ax.yaxis.offsetText.set_visible(False)
# loop over the top axes:
for ax in g.axes[:-1, :].flat:
# get the xticklabels from the axis and set visibility to False
for label in ax.get_xticklabels():
label.set_visible(False)
ax.xaxis.offsetText.set_visible(False)
Run Code Online (Sandbox Code Playgroud)
更新:
为了完整起见,mwaskom(参考github问题)给出了发生此问题的解释:
发生这种情况是因为 matplotlib 在移动脊柱时在内部调用 axis.reset_ticks() 。否则,脊柱会移动,但蜱虫会留在同一个地方。它在 matplotlib 中不可配置,即使可以配置,我也不知道是否有用于移动单个刻度的公共 API。不幸的是,我认为在偏移刺后你必须自己去除刻度标签。