Seaborn despine()带回了ytick标签

时间:2016-06-16 13:05:58

标签: seaborn

以下是代码段

with prevEndDate as
    (
        select t.contractID
        , t.userID
        , t.startDate
        , t.endDate
        , lag(t.endDate,1,NULL) over (partition by t.userID order by t.contractID asc) as prevEndDate
        from db_name.dbo.myTable as t   
    )
select p.contractID
, p.userID
, p.startDate
, p.endDate
, case when datediff(m,p.prevEndDate, p.startDate) >= 12 then 'True' else 'False' end as [12m Lapse]
from prevEndDate as p

使用以下输出

enter image description here

我希望将despine偏移引入这些图,同时保持其余不变。因此,我在现有代码中插入了 despine函数

tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, col = 'time')
g = g.map(plt.hist, "tip")

导致以下图

enter image description here

结果,偏移应用于轴。但是,右图上的 ytick标签又回来了,我不想要。

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

要删除yaxis刻度标签,您可以使用以下代码:

libs:

import seaborn as sns
sns.set_style('ticks')

调整后的代码:

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)

enter image description here

更一般的是,你现在拥有2x2 FacetGrid的图像,你想要偏移一个偏移,但x和yticklabels返回:

enter image description here

使用以下代码删除它们:

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)

enter image description here

更新:

为了完整性,mwaskom(ref to github issue)解释了为什么会出现这个问题:

  

所以发生这种情况是因为matplotlib在移动脊柱时会在内部调用axis.reset_ticks()。否则,脊椎会移动,但是蜱虫会停留在同一个地方。它在matplotlib中是不可配置的,即使它是,我也不知道是否有用于移动个别滴答的公共API。不幸的是,我认为你必须在抵消刺之后自己删除刻度标签。