From 80d0ac8361b44d6da5fb7d75e17eec03bfffde6a Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 2 Jan 2020 15:55:27 +0100 Subject: [PATCH] Backport PR #16006: Ignore pos in StrCategoryFormatter.__call__ to display correct label in the preview window MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ignore pos in StrCategoryFormatter.__call__ to display correct… (#16006) Ignore pos in StrCategoryFormatter.__call__ to display correct label in the preview window Conflicts: lib/matplotlib/tests/test_category.py - collision with py2 removal work (_to_str) --- lib/matplotlib/category.py | 7 ++++++- lib/matplotlib/tests/test_category.py | 8 ++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/category.py b/lib/matplotlib/category.py index 55af459fd575..9013460311cc 100644 --- a/lib/matplotlib/category.py +++ b/lib/matplotlib/category.py @@ -138,7 +138,12 @@ def __init__(self, units_mapping): self._units = units_mapping def __call__(self, x, pos=None): - return '' if pos is None else self.format_ticks([x])[0] + """ + Return the category label string for tick val *x*. + + The position *pos* is ignored. + """ + return self.format_ticks([x])[0] def format_ticks(self, values): r_mapping = {v: self._text(k) for k, v in self._units.items()} diff --git a/lib/matplotlib/tests/test_category.py b/lib/matplotlib/tests/test_category.py index 1fa38923d437..ee60482656f8 100644 --- a/lib/matplotlib/tests/test_category.py +++ b/lib/matplotlib/tests/test_category.py @@ -156,16 +156,16 @@ def test_StrCategoryFormatter(self, ax, ydata): unit = cat.UnitData(ydata) labels = cat.StrCategoryFormatter(unit._mapping) for i, d in enumerate(ydata): - assert labels(i, i) == _to_str(d) + assert labels(i, i) == d + assert labels(i, None) == d @pytest.mark.parametrize("ydata", cases, ids=ids) @pytest.mark.parametrize("plotter", PLOT_LIST, ids=PLOT_IDS) def test_StrCategoryFormatterPlot(self, ax, ydata, plotter): plotter(ax, range(len(ydata)), ydata) for i, d in enumerate(ydata): - assert ax.yaxis.major.formatter(i, i) == _to_str(d) - assert ax.yaxis.major.formatter(i+1, i+1) == "" - assert ax.yaxis.major.formatter(0, None) == "" + assert ax.yaxis.major.formatter(i) == d + assert ax.yaxis.major.formatter(i+1) == "" def axis_test(axis, labels):