Skip to content

YearLocator should prefer ticks at the turn of the decade #9838

Closed

Description

@has2k1

Bug summary

YearLocator is agnostic about decades. Ticks at the decades are nicer, so when choosing a starting value in some cases it should prefer to start a the turn of the decade or even mid decade. i.e 1970 instead of 1968, 1965 instead of 1964. This "smart" behaviour should apply when the intervals are factors of 5 and 10.

Code for reproduction

import datetime
import matplotlib.pyplot as plt

x = [datetime.datetime(1966, 1, 1),
     datetime.datetime(2017, 1, 1)]

ticks = [datetime.datetime(i, 1, 1)
        for i in range(1970, 2020, 10)]

y = np.arange(len(x))
fig, ax = plt.subplots()
ax.plot_date(x, y)
# ax.set_xticks(ticks)  # preferred ticks

Actual outcome
bad-date-ticks

Better outcome
good-date-ticks

Activity

  1. dstansby commented on Nov 23, 2017

    @dstansby
    Member

    Probably related to #9801

  2. has2k1 commented on Nov 23, 2017

    @has2k1
    ContributorAuthor

    Yes I think the motivation behind both issues is the same, but the solution (so far) does not work for this case.

  3. jklymak commented on Nov 23, 2017

    @jklymak
    Member

    Yeah, the logic for years is different because you don't know the range of years to start with. But, this should be easy to fix with a bit of extra logic (I think).

    import datetime
    import matplotlib.dates as mdates
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = [datetime.datetime(1966, 1, 1),
         datetime.datetime(2017, 1, 1)]
    
    ticks = [datetime.datetime(i, 1, 1)
            for i in range(1970, 2020, 10)]
    
    y = np.arange(len(x))
    fig, ax = plt.subplots()
    ax.plot_date(x, y)
    locator = mdates.AutoDateLocator(interval_multiples=True)
    ax.xaxis.set_major_locator(locator)
    plt.show()
  4. added this to the milestone on Nov 23, 2017
  5. modified the milestones: , v2.2.0 on Feb 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions