diff --git a/docs/iris/src/whatsnew/latest.rst b/docs/iris/src/whatsnew/latest.rst index 91330715da..7b03d4aeec 100644 --- a/docs/iris/src/whatsnew/latest.rst +++ b/docs/iris/src/whatsnew/latest.rst @@ -97,6 +97,10 @@ This document explains the changes made to Iris for this release which was previously failing for some coordinate systems. See :issue:`3629`. (:pull:`3804`) +* `@stephenworsley`_ changed the way tick labels are assigned from string coords. + Previously, the first tick label would occasionally be duplicated. This also + removes the use of Matplotlib's deprecated ``IndexFormatter``. (:pull:`3857`) + 💣 Incompatible Changes ======================= diff --git a/lib/iris/plot.py b/lib/iris/plot.py index 0500c3d890..b489bba732 100644 --- a/lib/iris/plot.py +++ b/lib/iris/plot.py @@ -249,7 +249,17 @@ def _string_coord_axis_tick_labels(string_axes, axes=None): ax = axes if axes else plt.gca() for axis, ticks in string_axes.items(): - formatter = mpl_ticker.IndexFormatter(ticks) + # Define a tick formatter. This will assign a label to all ticks + # located precisely on an integer in range(len(ticks)) and assign + # an empty string to any other ticks. + def ticker_func(tick_location, _): + tick_locations = range(len(ticks)) + labels = ticks + label_dict = dict(zip(tick_locations, labels)) + label = label_dict.get(tick_location, "") + return label + + formatter = mpl_ticker.FuncFormatter(ticker_func) locator = mpl_ticker.MaxNLocator(integer=True) this_axis = getattr(ax, axis) this_axis.set_major_formatter(formatter) diff --git a/lib/iris/tests/unit/plot/__init__.py b/lib/iris/tests/unit/plot/__init__.py index ad74fc253e..f493ba249a 100644 --- a/lib/iris/tests/unit/plot/__init__.py +++ b/lib/iris/tests/unit/plot/__init__.py @@ -44,7 +44,7 @@ def tick_loc_and_label(self, axis_name, axes=None): def assertBoundsTickLabels(self, axis, axes=None): actual = self.tick_loc_and_label(axis, axes) expected = [ - (-1.0, "a"), + (-1.0, ""), (0.0, "a"), (1.0, "b"), (2.0, "c"),