1

I have included the screenshot of the plot. Is there a way to prevent seaborn from skipping the xtick labels in timeseries data.

Timeseries plot skipping the years in x axis.

gannu
  • 105
  • 1
  • 9

2 Answers2

1

Most seaborn functions return a matplotlib object, so you can control the number of major ticks displayed via matplotlib. By default, matplotlib will auto-scale, which is why it hides some year labels, you can try to set the MaxNLocator.

Consider the following example:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# load data
df = sns.load_dataset('flights')
df.drop_duplicates('year', inplace=True)
df.year = df.year.astype('str')

# plot
fig, ax = plt.subplots(figsize=(5, 2))
sns.lineplot(x='year', y='passengers', data=df, ax=ax)
ax.xaxis.set_major_locator(plt.MaxNLocator(5))

This gives you:

enter image description here

ax.xaxis.set_major_locator(plt.MaxNLocator(10))

will give you

enter image description here

steven
  • 2,130
  • 19
  • 38
0

Agree with answer of @steven, just want to say that methods for xticks like plt.xticks or ax.xaxis.set_ticks seem more natural to me. Full details can be found here.

Victor Luu
  • 244
  • 2
  • 10
  • `plt.xticks` is a top-level function, but you need to access and control the axes return by seaborn here. `set_ticks` needs you to define tickes. So they are not really easy to use. – steven Jul 02 '20 at 01:31