diff --git a/doc/api/next_api_changes/behavior/28501-OV.rst b/doc/api/next_api_changes/behavior/28501-OV.rst new file mode 100644 index 000000000000..cc816e55f696 --- /dev/null +++ b/doc/api/next_api_changes/behavior/28501-OV.rst @@ -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. diff --git a/lib/matplotlib/dates.py b/lib/matplotlib/dates.py index c12d9f31ba4b..15de61f69df7 100644 --- a/lib/matplotlib/dates.py +++ b/lib/matplotlib/dates.py @@ -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: diff --git a/lib/matplotlib/tests/test_dates.py b/lib/matplotlib/tests/test_dates.py index 4133524e0e1a..2d60e3525b2a 100644 --- a/lib/matplotlib/tests/test_dates.py +++ b/lib/matplotlib/tests/test_dates.py @@ -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