The problem arises when plotting a time series with irregularly-spaced samples:
-
I created two random time series with equally-spaced timestamps. Asking pandas to show it within different subplots works fine and provides correct date ticks.
-
I randomly dropped some samples and showed the time series in another set of subplots. This time, the minor ticks appear at random positions.
import numpy as np
import pandas as pd
# Create dataframe
time = pd.date_range("2007-04", "2019-03", freq="M")
data = np.random.rand(len(time), 2)
df = pd.DataFrame(data=data, index=time)
# Show with equally-spaced samples (case 1)
df.plot(subplots=True)
# Show with unevenly-spaced samples (case 2)
df = df.sample(frac=0.8, random_state=200)
df.plot(subplots=True)
Case 1: evenly-sampled time series
Case 2: unevenly-sampled time series
I also tried to include the following lines without success.
dateticks = mdates.AutoDateLocator()
dateticklabels = mdates.ConciseDateFormatter(dateticks)
ax[-1].xaxis.set_major_locator(dateticks)
ax[-1].xaxis.set_major_formatter(dateticklabels)
Notes
I am using pandas 1.4.1
with matplotlib 3.5.1
and latest macOS version.