Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
leonard-seydoux opened this issue May 7, 2022 · 0 comments
Open
Labels

Comments

@leonard-seydoux
Copy link

leonard-seydoux commented May 7, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants