'Pandas subplot date ticks appear unevenly spaced with irregular time series
I created this example after seeing the issue multiple times. This helped me realize that the problem comes when plotting the time series of a data frame with irregularly-spaced samples. The example below illustrate the issue.
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)
So the questions are: would anyone know if there is a plan to fix this issue and if any solution would help solve it meanwhile?
Note
I am using pandas 1.4.1
with matplotlib 3.5.1
and latest macOS version.
Edit
Issue opened on GitHub: https://github.com/pandas-dev/pandas/issues/46961
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|