I have included the screenshot of the plot. Is there a way to prevent seaborn from skipping the xtick labels in timeseries data.
Asked
Active
Viewed 973 times
1
-
1Please share your code so that we can help you. – Daniele Cappuccio Jul 01 '20 at 17:12
-
@DanieleCappuccio code is as below: sns.lineplot(x='ReqeustYear',y='Values', data=df). – gannu Jul 01 '20 at 17:56
-
This question is already replied [here](https://stackoverflow.com/questions/12608788/changing-the-tick-frequency-on-x-or-y-axis-in-matplotlib?rq=1). – Victor Luu Jul 02 '20 at 00:31
2 Answers
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:
ax.xaxis.set_major_locator(plt.MaxNLocator(10))
will give you

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