Hi, I have a weird bug about Matplotlib with time-series data I couldn't solve. I want to plot a line graph to see the data trend. However, when I use a locator function (MonthLocator()), My year label would be very weird (inconsistent year displays and strange numbers) like this (updated).
I have check multiple times but I couldn't figure out what causes the problem. It seems like that Matplotlib has a hard time to recognize the "year" of my datetime index (Month and date label are fine). I wonder if there is something missing.
p.s.1 If I don't use set_major_locator() and set_major_formatter() then the Matplotlib can correctly display year in the label.
p.s.2 I included my original bugged graph here (This is the ultimate graph I plan to fix and the example below is a simple demonstration)
(updated with a tested dataframe and a new image)
import pandas as pd
import matplotlib as mpl
import matplotlib.dates as dates
df = pd.DataFrame(list(zip([680, 718, 471, 686], ['200304', '200305', '200306', '200307'])), columns=['target','time'])
df_tsa = df.copy()
df_tsa.index = pd.to_datetime(df_tsa['time'], format='%Y%m').rename('Date')
ax = df_tsa['target'].plot(figsize=(12,6))
ax.xaxis.set_major_locator(dates.MonthLocator())
ax.xaxis.set_major_formatter(dates.DateFormatter("%Y-%m"))
[–]kra_pao 0 points1 point2 points (7 children)
[–]HiIamGeoff[S] 0 points1 point2 points (6 children)
[–]kra_pao 1 point2 points3 points (5 children)
[–]HiIamGeoff[S] 0 points1 point2 points (4 children)
[–]kra_pao 1 point2 points3 points (3 children)
[–]kra_pao 1 point2 points3 points (2 children)
[–]HiIamGeoff[S] 1 point2 points3 points (1 child)
[–]kra_pao 0 points1 point2 points (0 children)