Skip to content

Off-by-one bug in annual axis labels when localized time crosses year boundary #12675

Closed

Description

@paulgb

Bug report

Bug summary

When plotting dates with time zone information on the x-axis, under certain circumstances, the wrong year will be displayed as the label.

Code for reproduction

import matplotlib.pyplot as plt
import pytz
from datetime import datetime, timedelta

x = [datetime(2010, 1, 1, tzinfo=pytz.timezone('America/New_York')) + timedelta(i) for i in range(2000)]
plt.plot(x, [0] * len(x));

Actual outcome

The first label on the plot is 2009.

Expected outcome

The first label on the plot is 2010.

Matplotlib version

Built this morning from master. Also confirmed the issue on 3.0.0 and confirmed that 2.2.2 does not have the issue (or at least not with the reproduction steps I found.)

  • Operating system: Debian 7.11
  • Matplotlib version: from source
  • Matplotlib backend module://ipykernel.pylab.backend_inline
  • Python version: 3.7.0
  • Jupyter version (if applicable): 4.4.0
  • Other libraries: pytz 2018.5

I built matplotlib from source to verify that this bug affects master. The checks against 3.0.0 and 2.2.2 were done on a conda install of those versions and may have had different versions of pytz and jupyter.

Activity

  1. jklymak commented on Oct 30, 2018

    @jklymak
    Member

    OK, this bisects to 0337883 (#9801). Not sure why that causes this bug, but it is certainly a bad one.

  2. self-assigned this
    on Oct 30, 2018
  3. added this to the v3.0.x milestone on Oct 30, 2018
  4. ImportanceOfBeingErnest commented on Oct 30, 2018

    @ImportanceOfBeingErnest
    Member

    A workaround is to set the interval_multiples to False.

    plt.gca().xaxis.get_major_locator().interval_multiples = False
    

    But that's sure not a sustainable solution.

    I wonder if this goes deeper, because without the time zone it works fine.

  5. jklymak commented on Oct 30, 2018

    @jklymak
    Member

    Yeah, I think #9801 just made the real bug more apparent by changing the default interval_multiples

  6. jklymak commented on Oct 30, 2018

    @jklymak
    Member

    OK, this seems to be either a datetime bug or pytz bug?

    import pytz
    from datetime import datetime, timedelta, timezone
    UTC = timezone.utc
    
    date0 = datetime(2010, 1, 1, tzinfo=pytz.timezone('US/Pacific'))
    print('date0    ', date0)
    dateUTC = date0.astimezone(tz=UTC)
    print('dateUTC  ', dateUTC)
    dateLocal = dateUTC.astimezone(tz=pytz.timezone('US/Pacific'))
    print('datelocal', dateLocal)

    yields:

    date0     2010-01-01 00:00:00-07:53
    dateUTC   2010-01-01 07:53:00+00:00
    datelocal 2009-12-31 23:53:00-08:00
    
  7. jklymak commented on Oct 30, 2018

    @jklymak
    Member

    Ah, OK, you can't do:

    datetime(2010, 1, 1, tzinfo=pytz.timezone('America/New_York'))
    

    but you can do:

    datetime(2010, 1, 1).astimezone(pytz.timezone('America/New_York'))
    

    Doing

    date0 = datetime(2010, 1, 1, 0,0,0).astimezone(pytz.timezone('US/Pacific'))
    print('date0    ', date0)
    dateUTC = date0.astimezone(tz=UTC)
    print('dateUTC  ', dateUTC)
    dateLocal = dateUTC.astimezone(tz=pytz.timezone('US/Pacific'))
    print('datelocal', dateLocal)

    yields a good round trip..

    date0     2010-01-01 00:00:00-08:00
    dateUTC   2010-01-01 08:00:00+00:00
    datelocal 2010-01-01 00:00:00-08:00
    

    OTOH, the bug described above is still an issue...

  8. jklymak commented on Oct 30, 2018

    @jklymak
    Member

    OK, thanks for the bug report - there was a tz argument not being passed through. Fixed in #12678

  9. modified the milestones: v3.0.2, v3.0.3 on Nov 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions