Skip to content

[Bug]: ConciseDateFormatter doesn't handle DST changes correctly #29160

@otourzan

Description

@otourzan

Bug summary

When the time series to plot has a day light saving change and the time zone of data changes, the ticker labels formatted incorrectly. It seems to format time zone part instead of the correct base, in this example the day.

Code for reproduction

import datetime

import matplotlib.pyplot as plt
import numpy as np

import matplotlib.dates as mdates
from zoneinfo import ZoneInfo

base = datetime.datetime(2005, 2, 1)
dates = [base + datetime.timedelta(hours=(2 * i)) for i in range(732)]
N = len(dates)
np.random.seed(19680801)
y = np.cumsum(np.random.randn(N))

fig, axs = plt.subplots(3, 1, layout='constrained', figsize=(6, 6))
lims = [(np.datetime64('2024-10-27'), np.datetime64('2024-11-10')),
        (np.datetime64('2024-11-11'), np.datetime64('2024-11-22')),
        (np.datetime64('2005-02-03 11:00'), np.datetime64('2005-02-04 13:20'))
        ]


fig, axs = plt.subplots(3, 1, layout='constrained', figsize=(6, 6))

for nn, ax in enumerate(axs):
    # locator = mdates.AutoDateLocator()
    locator = mdates.AutoDateLocator(tz=ZoneInfo("America/Los_Angeles"))
    formatter = mdates.ConciseDateFormatter(locator)
    formatter.formats = ['%y',  # ticks are mostly years
                         '%b',       # ticks are mostly months
                         '%d',       # ticks are mostly days
                         '%H:%M',    # hrs
                         '%H:%M',    # min
                         '%S.%f', ]  # secs
    # these are mostly just the level above...
    formatter.zero_formats = [''] + formatter.formats[:-1]
    # ...except for ticks that are mostly hours, then it is nice to have
    # month-day:
    formatter.zero_formats[3] = '%d-%b'

    formatter.offset_formats = ['',
                                '%Y',
                                '%b %Y',
                                '%d %b %Y',
                                '%d %b %Y',
                                '%d %b %Y %H:%M', ]
    ax.xaxis.set_major_locator(locator)
    ax.xaxis.set_major_formatter(formatter)

    ax.plot(dates, y)
    ax.set_xlim(lims[nn])
axs[0].set_title('Concise Date Formatter')

plt.show()

Actual outcome

Screenshot 2024-11-20 at 4 19 38 PM

Expected outcome

2nd graph of the same image shows the expected tick labels in the graph.

Additional information

No response

Operating system

No response

Matplotlib Version

3.9.2

Matplotlib Backend

macosx

Python version

Python 3.11.6

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions