Huey
Huey

Reputation: 2834

Remove some xtick labels from a seaborn plot

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?

enter image description here

Upvotes: 13

Views: 9403

Answers (2)

Trenton McKinney
Trenton McKinney

Reputation: 62503

  • factorplot is intended for use when the main independent variable is categorical, and has been renamed to catplot.
  • The accepted answer, in general, offers a solution for spacing the xticks.
  • The primary issue is this should be a quantitative plot, not a categorical plot, and is why there's an xtick for every category.
  • The primary takeaway is to use the correct type of plot, categorical, or quantitative.
    • categorical plots will always show all of the xticks.
    • quantitative plots will usually format the step size of the xticks.
      • All xticks will be shown if the values used for the x-axis are 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)

enter image description here

  • Without 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()

enter image description here

Upvotes: 0

mechanical_meat
mechanical_meat

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()

enter image description here


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()

enter image description here

Upvotes: 22

Related Questions