Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 64d38d6

Browse filesBrowse files
authored
Merge pull request #15588 from meeseeksmachine/auto-backport-of-pr-15478-on-v3.2.x
Backport PR #15478 on branch v3.2.x (Make ConciseDateFormatter obey timezone)
2 parents 2527d3b + 1ea8317 commit 64d38d6
Copy full SHA for 64d38d6

File tree

Expand file treeCollapse file tree

2 files changed

+45
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+45
-2
lines changed

‎lib/matplotlib/dates.py

Copy file name to clipboardExpand all lines: lib/matplotlib/dates.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ def __call__(self, x, pos=None):
769769
return formatter(x, pos=pos)
770770

771771
def format_ticks(self, values):
772-
tickdatetime = [num2date(value) for value in values]
772+
tickdatetime = [num2date(value, tz=self._tz) for value in values]
773773
tickdate = np.array([tdt.timetuple()[:6] for tdt in tickdatetime])
774774

775775
# basic algorithm:
@@ -835,7 +835,7 @@ def get_offset(self):
835835
return self.offset_string
836836

837837
def format_data_short(self, value):
838-
return num2date(value).strftime('%Y-%m-%d %H:%M:%S')
838+
return num2date(value, tz=self._tz).strftime('%Y-%m-%d %H:%M:%S')
839839

840840

841841
class AutoDateFormatter(ticker.Formatter):

‎lib/matplotlib/tests/test_dates.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_dates.py
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,49 @@ def _create_auto_date_locator(date1, date2):
512512
assert strings == expected
513513

514514

515+
def test_concise_formatter_tz():
516+
def _create_auto_date_locator(date1, date2, tz):
517+
fig, ax = plt.subplots()
518+
519+
locator = mdates.AutoDateLocator(interval_multiples=True)
520+
formatter = mdates.ConciseDateFormatter(locator, tz=tz)
521+
ax.yaxis.set_major_locator(locator)
522+
ax.yaxis.set_major_formatter(formatter)
523+
ax.set_ylim(date1, date2)
524+
fig.canvas.draw()
525+
sts = []
526+
for st in ax.get_yticklabels():
527+
sts += [st.get_text()]
528+
return sts, ax.yaxis.get_offset_text().get_text()
529+
530+
d1 = datetime.datetime(1997, 1, 1).replace(tzinfo=datetime.timezone.utc)
531+
results = ([datetime.timedelta(hours=40),
532+
['03:00', '07:00', '11:00', '15:00', '19:00', '23:00',
533+
'03:00', '07:00', '11:00', '15:00', '19:00'],
534+
"1997-Jan-02"
535+
],
536+
[datetime.timedelta(minutes=20),
537+
['03:00', '03:05', '03:10', '03:15', '03:20'],
538+
"1997-Jan-01"
539+
],
540+
[datetime.timedelta(seconds=40),
541+
['03:00', '05', '10', '15', '20', '25', '30', '35', '40'],
542+
"1997-Jan-01 03:00"
543+
],
544+
[datetime.timedelta(seconds=2),
545+
['59.5', '03:00', '00.5', '01.0', '01.5', '02.0', '02.5'],
546+
"1997-Jan-01 03:00"
547+
],
548+
)
549+
550+
new_tz = datetime.timezone(datetime.timedelta(hours=3))
551+
for t_delta, expected_strings, expected_offset in results:
552+
d2 = d1 + t_delta
553+
strings, offset = _create_auto_date_locator(d1, d2, new_tz)
554+
assert strings == expected_strings
555+
assert offset == expected_offset
556+
557+
515558
def test_auto_date_locator_intmult_tz():
516559
def _create_auto_date_locator(date1, date2, tz):
517560
locator = mdates.AutoDateLocator(interval_multiples=True, tz=tz)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.