Skip to content

Subplot date ticks appear unevenly spaced with irregular time series #46961

@leonard-seydoux

Description

@leonard-seydoux

The problem arises when plotting a time series with irregularly-spaced samples:

  1. 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.

  2. 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

pandas-subplots-evenly-sampled-correct-dateticks

Case 2: unevenly-sampled time series

pandas-subplots-unevenly-sampled-wrong-dateticks

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions