Skip to content

Commit 436a12a

Browse files
authored
ConciseDateFormatter's offset string is correct on an inverted axis (matplotlib#28501)
1 parent 85d7295 commit 436a12a

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The offset string associated with ConciseDateFormatter will now invert when the axis is inverted
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
Previously, when the axis was inverted, the offset string associated with ConciseDateFormatter would not change,
4+
so the offset string indicated the axis was oriented in the wrong direction. Now, when the axis is inverted, the offset
5+
string is oriented correctly.

lib/matplotlib/dates.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,12 @@ def format_ticks(self, values):
796796

797797
if show_offset:
798798
# set the offset string:
799-
self.offset_string = tickdatetime[-1].strftime(offsetfmts[level])
799+
if (self._locator.axis and
800+
self._locator.axis.__name__ in ('xaxis', 'yaxis')
801+
and self._locator.axis.get_inverted()):
802+
self.offset_string = tickdatetime[0].strftime(offsetfmts[level])
803+
else:
804+
self.offset_string = tickdatetime[-1].strftime(offsetfmts[level])
800805
if self._usetex:
801806
self.offset_string = _wrap_in_tex(self.offset_string)
802807
else:

lib/matplotlib/tests/test_dates.py

+17
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,23 @@ def test_concise_formatter_show_offset(t_delta, expected):
636636
assert formatter.get_offset() == expected
637637

638638

639+
def test_concise_formatter_show_offset_inverted():
640+
# Test for github issue #28481
641+
d1 = datetime.datetime(1997, 1, 1)
642+
d2 = d1 + datetime.timedelta(days=60)
643+
644+
fig, ax = plt.subplots()
645+
locator = mdates.AutoDateLocator()
646+
formatter = mdates.ConciseDateFormatter(locator)
647+
ax.xaxis.set_major_locator(locator)
648+
ax.xaxis.set_major_formatter(formatter)
649+
ax.invert_xaxis()
650+
651+
ax.plot([d1, d2], [0, 0])
652+
fig.canvas.draw()
653+
assert formatter.get_offset() == '1997-Jan'
654+
655+
639656
def test_concise_converter_stays():
640657
# This test demonstrates problems introduced by gh-23417 (reverted in gh-25278)
641658
# In particular, downstream libraries like Pandas had their designated converters

0 commit comments

Comments
 (0)