0

my matplotlib version is 3.0.0

import matplotlib.pyplot as plt
import matplotlib.mdates as mdates
import pandas as pd

years = range(2016, 2025)
fig, axes = plt.subplots(len(years), 1, figsize=(10, 20))

for ax, year in zip(axes, years):
    common_idx = pd.date_range(str(year), str(year+1), closed='left')
    s = pd.Series(42, common_idx)
    s.plot(ax=ax)

    ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d'))
    ax.xaxis.set_major_locator(mdates.MonthLocator())
    ax.set_title(year)

fig.set_tight_layout(True)

the above code is mcve. and the below is the result.

enter image description here

why do graphs in 2016, 2018, 2019, 2020, 2022, 2023 put the first tick on the wrong location? I can't even find any pattern (like leap year)...

0xF4D3C0D3
  • 747
  • 1
  • 5
  • 15
  • 1
    In general the datetime utilities of pandas and matplotlib are incompatible. If you want to use the datetime locators from `matplotlib.dates` you would need to pass the `x_compat=True` option in pandas plotting methods. See [duplicate](https://stackoverflow.com/questions/44213781/pandas-dataframe-line-plot-display-date-on-xaxis). – ImportanceOfBeingErnest Nov 15 '19 at 02:56
  • @ImportanceOfBeingErnest First of all, sorry for the late reply and second you saved my day! Finally, really really thank you! – 0xF4D3C0D3 Nov 17 '19 at 15:12

0 Answers0