【问题标题】:Remove some x labels with Seaborn使用 Seaborn 删除一些 x 标签
【发布时间】:2016-12-13 00:56:59
【问题描述】:

在下面的屏幕截图中,我所有的 x 标签都相互重叠。

g = sns.factorplot(x='Age', y='PassengerId', hue='Survived', col='Sex', kind='strip', data=train);

我知道我可以通过调用 g.set(xticks=[]) 来删除所有标签,但是有没有办法只显示一些年龄标签,例如 0、20、40、60、80?

【问题讨论】:

    标签: python seaborn


    【解决方案1】:

    我不知道为什么没有像 y 轴上那样合理的默认刻度和值。无论如何,您都可以执行以下操作:

    import seaborn as sns
    import matplotlib.pyplot as plt
    import matplotlib.ticker as ticker
    
    titanic = sns.load_dataset('titanic')
    sns.factorplot(x='age',y='fare',hue='survived',col='sex',data=titanic,kind='strip')
    ax = plt.gca()
    ax.xaxis.set_major_formatter(ticker.FormatStrFormatter('%d'))
    ax.xaxis.set_major_locator(ticker.MultipleLocator(base=20))
    plt.show()
    

    结果:

    【讨论】:

    • 感谢您的回答。您能否解释一下FormatStrFormatter('%d') 的工作原理?更具体地说,%d 是如何工作的,还有其他选择吗?
    • @AliAsgari:FormatStrFormatter 实例是提供给set_major_formatter 所必需的。 %d 来自此处的格式规范迷你语言:docs.python.org/3/library/string.html#formatspec。我不知道有任何替代方案。
    猜你喜欢
    • 2020-07-05
    • 2020-11-05
    • 2011-11-12
    • 2020-02-16
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 2014-12-01
    • 1970-01-01
    相关资源
    最近更新 更多