Skip to content

Commit

Permalink
ConciseDateFormatter's offset string is correct on an inverted axis (m…
Browse files Browse the repository at this point in the history
  • Loading branch information
OdileVidrine authored Aug 21, 2024
1 parent 85d7295 commit 436a12a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions doc/api/next_api_changes/behavior/28501-OV.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The offset string associated with ConciseDateFormatter will now invert when the axis is inverted
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Previously, when the axis was inverted, the offset string associated with ConciseDateFormatter would not change,
so the offset string indicated the axis was oriented in the wrong direction. Now, when the axis is inverted, the offset
string is oriented correctly.
7 changes: 6 additions & 1 deletion lib/matplotlib/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,12 @@ def format_ticks(self, values):

if show_offset:
# set the offset string:
self.offset_string = tickdatetime[-1].strftime(offsetfmts[level])
if (self._locator.axis and
self._locator.axis.__name__ in ('xaxis', 'yaxis')
and self._locator.axis.get_inverted()):
self.offset_string = tickdatetime[0].strftime(offsetfmts[level])
else:
self.offset_string = tickdatetime[-1].strftime(offsetfmts[level])
if self._usetex:
self.offset_string = _wrap_in_tex(self.offset_string)
else:
Expand Down
17 changes: 17 additions & 0 deletions lib/matplotlib/tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,23 @@ def test_concise_formatter_show_offset(t_delta, expected):
assert formatter.get_offset() == expected


def test_concise_formatter_show_offset_inverted():
# Test for github issue #28481
d1 = datetime.datetime(1997, 1, 1)
d2 = d1 + datetime.timedelta(days=60)

fig, ax = plt.subplots()
locator = mdates.AutoDateLocator()
formatter = mdates.ConciseDateFormatter(locator)
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
ax.invert_xaxis()

ax.plot([d1, d2], [0, 0])
fig.canvas.draw()
assert formatter.get_offset() == '1997-Jan'


def test_concise_converter_stays():
# This test demonstrates problems introduced by gh-23417 (reverted in gh-25278)
# In particular, downstream libraries like Pandas had their designated converters
Expand Down

0 comments on commit 436a12a

Please sign in to comment.