Skip to content

Commit

Permalink
Replace deprecated IndexFormatter (#3857)
Browse files Browse the repository at this point in the history
* Replace deprecated IndexFormatter

* Blacken

* Add Whats New

* Clarify tick formatter comments
  • Loading branch information
stephenworsley committed Sep 16, 2020
1 parent 3a18866 commit c49af3b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/iris/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=======================
Expand Down
12 changes: 11 additions & 1 deletion lib/iris/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/iris/tests/unit/plot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit c49af3b

Please sign in to comment.