Reputation: 2834
In the screenshot below, all my x-labels are overlapping each other.
g = sns.factorplot(x='Age', y='PassengerId', hue='Survived', col='Sex', kind='strip', data=train);
I know that I can remove all the labels by calling g.set(xticks=[])
, but is there a way to just show some of the Age labels, like 0, 20, 40, 60, 80?
Upvotes: 13
Views: 9403
Reputation: 62503
factorplot
is intended for use when the main independent variable is categorical, and has been renamed to catplot
.
factorplot
.catplot
.relplot
with kind='scatter'
.
strings
, instead of datetime
or numeric
.import seaborn as sns
titanic = sns.load_dataset('titanic')
g = sns.relplot(x='age', y='fare', hue='survived', col='sex', data=titanic)
relplot
and scatterplot
, this could have been plotted directly with pandas.DataFrame.plot
(or ax.scatter
), but this is a more complex implementation.import seaborn as sns
import matplotlib.pyplot as plt
titanic = sns.load_dataset('titanic')
fig, axes = plt.subplots(1, 2, figsize=(12, 5), sharex=True)
axes = axes.flat
for ax, (sex, data) in zip(axes, titanic.groupby('sex')[['survived', 'age', 'fare']]):
ax.spines[['top', 'right']].set_visible(False)
for (survived, sel), color in zip(data.groupby('survived'), ['tab:blue', 'tab:orange']):
sel.plot(kind='scatter', x='age', y='fare', ec='w', s=30,
color=color, title=sex.title(), label=survived, ax=ax)
axes[0].get_legend().remove()
axes[1].legend(title='Survived', bbox_to_anchor=(1, 0.5), loc='center left', frameon=False)
plt.show()
Upvotes: 0
Reputation: 169444
I am not sure why there aren't sensible default ticks and values like there are on the y-axis.
The FormatStrFormatter
instance is necessary to supply set_major_formatter
. The %d
is from the format specification mini-language.
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()
This also works with catplot
, which replaced factorplot
titanic = sns.load_dataset('titanic')
sns.catplot(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()
Upvotes: 22