Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 59cbf83

Browse files
committed
Change behavior of get_locator to accurately calculate number of days between two points.
Signed-off-by: Paul G <[email protected]>
1 parent 643b440 commit 59cbf83

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

lib/matplotlib/dates.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ def _from_ordinalf(x, tz=None):
257257

258258
dt += datetime.timedelta(seconds=remainder * SEC_PER_DAY)
259259

260+
# Compensate for rounding errors
261+
if dt.microsecond < 10:
262+
dt = dt.replace(microsecond=0)
263+
elif dt.microsecond > 999990:
264+
dt += datetime.timedelta(microseconds=1e6 - dt.microsecond)
265+
260266
return dt
261267

262268

@@ -944,14 +950,15 @@ def autoscale(self):
944950
def get_locator(self, dmin, dmax):
945951
'Pick the best locator based on a distance.'
946952
delta = relativedelta(dmax, dmin)
953+
tdelta = dmax - dmin
947954

948955
# take absolute difference
949956
if dmin > dmax:
950957
delta = -delta
951958

952-
numYears = (delta.years * 1.0)
959+
numYears = delta.years * 1.0
953960
numMonths = (numYears * MONTHS_PER_YEAR) + delta.months
954-
numDays = (numMonths * DAYS_PER_MONTH) + delta.days
961+
numDays = tdelta.days # Avoid estimates of days/month, days/year
955962
numHours = (numDays * HOURS_PER_DAY) + delta.hours
956963
numMinutes = (numHours * MIN_PER_HOUR) + delta.minutes
957964
numSeconds = (numMinutes * SEC_PER_MIN) + delta.seconds

0 commit comments

Comments
 (0)