【发布时间】: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?
【问题讨论】:
在下面的屏幕截图中,我所有的 x 标签都相互重叠。
g = sns.factorplot(x='Age', y='PassengerId', hue='Survived', col='Sex', kind='strip', data=train);
我知道我可以通过调用 g.set(xticks=[]) 来删除所有标签,但是有没有办法只显示一些年龄标签,例如 0、20、40、60、80?
【问题讨论】:
我不知道为什么没有像 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 是如何工作的,还有其他选择吗?
FormatStrFormatter 实例是提供给set_major_formatter 所必需的。 %d 来自此处的格式规范迷你语言:docs.python.org/3/library/string.html#formatspec。我不知道有任何替代方案。