ticker.MultipleLocator shows wrong xticks
sns.set_style("whitegrid")
bar,ax = plt.subplots(figsize=(32,18))
ax = sns.barplot(x="month_year", y="load", data=df, ci=None, palette="muted", orient='v')
ax.set_xlabel ("Date", fontsize=20)
ax.tick_params(axis="x", labelsize=25, rotation=45)
#ax.xaxis.set_major_locator(ticker.MultipleLocator(base=5))
ax.tick_params(axis="y", labelsize=25)
plt.show()
I would like to visualize xticks rarely. It looks like this:
When I use ax.xaxis.set_major_locator(ticker.MultipleLocator(base=5))
code it looks like this:
The case is the xticks appear wrong because last date should be 2021-12 but it is seen 2013-4.
How can I fix it?
Thanks in advance
The multipleLocator sets the scale interval to 5, but I think the reason is that the scale given is not in units of 5. I'm sure there are many other ways to do this, but I created a new tick interval and set the ticks to the new 5 unit interval.
import pandas as pd
import numpy as np
import seaborn as sns
df = pd.DataFrame({'date': pd.date_range('2011-02-01','2021-12-01', freq='M'),
'load': np.random.randint(15,150,130)})
df['month_year'] = pd.to_datetime(df['date'])
sns.set_style("whitegrid")
fig,ax = plt.subplots(figsize=(32,18))
ax = sns.barplot(x="month_year", y="load", data=df, ci=None, palette="muted", orient='v')
ax.tick_params(axis="x", labelsize=25, rotation=45)
ax.set_xticks(np.arange(0,130,5))
ax.set_xticklabels([x.strftime('%Y-%m') for x in df['month_year']][::5])
plt.show()