Home > Enterprise >  ConciseDateFormatter in subplots
ConciseDateFormatter in subplots

Time:10-16

Using mdates.ConciseDateFormatter in multiple subplots gives erroneous offset in the axis:

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

dti = pd.to_datetime(["2016-08-31", "2016-09-30"])
ts0 = pd.DataFrame({"x": [0, 1]}, index=dti)
ts1 = pd.DataFrame({"x": [0, 1]}, index=dti   pd.Timedelta(365, "D"))
fig, axs = plt.subplots(2, 1, sharex=False)
ts0["x"].plot(ax=axs[0], marker="o")
ts1["x"].plot(ax=axs[1], marker="o")
dlocator = mdates.AutoDateLocator(minticks=6, maxticks=9)
axs[0].xaxis.set_major_locator(dlocator)
axs[0].xaxis.set_major_formatter(mdates.ConciseDateFormatter(dlocator))
axs[1].xaxis.set_major_locator(dlocator)
axs[1].xaxis.set_major_formatter(mdates.ConciseDateFormatter(dlocator))

mdates_bug

The figure shows the x-axis tick labels for the top plot got messed up when assigning the major locator and formatter to the bottom plot. Is there any workaround to this bug or problem?

CodePudding user response:

  • It is required to instantiate AutoDateLocator for each axes
    • See enter image description here

  • Related