Skip to content

Incorrect labels returned with custom formatter and locator #9397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
astrofrog opened this issue Oct 13, 2017 · 2 comments
Closed

Incorrect labels returned with custom formatter and locator #9397

astrofrog opened this issue Oct 13, 2017 · 2 comments
Milestone

Comments

@astrofrog
Copy link
Contributor

astrofrog commented Oct 13, 2017

In the following example I am getting back the wrong labels from ax.xaxis.get_ticklabels:

import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter, MaxNLocator

ax = plt.subplot(1,1,1)

def formatter_func(pos, *args):
    return 'a' if pos == 1 else ''

ax.figure.canvas.draw()
print([tick.get_text() for tick in ax.xaxis.get_ticklabels()])

ax.xaxis.set_major_formatter(FuncFormatter(formatter_func))
ax.xaxis.set_major_locator(MaxNLocator(10, integer=True))

ax.set_xlim(-0.5, 2.5)

ax.figure.canvas.draw()
print([tick.get_text() for tick in ax.xaxis.get_ticklabels()])

This gives:

['0.0', '0.2', '0.4', '0.6', '0.8', '1.0']
['0.0', '', 'a', '', '0.8']

whereas I expected:

['0.0', '0.2', '0.4', '0.6', '0.8', '1.0']
['', '', 'a', '', '']
@afvincent
Copy link
Contributor

FWIW, if it is not the correct behavior, I was able to reproduce @astrofog's output on Matplotlib 1.5.3 (on GNU/Linux with Python 3.6.2 from conda)...

Just in case, looking at FuncFormatter docstring, the expected signature is formatter_func(x, pos): @astrofrog is this consistent with what you intended?

@anntzer
Copy link
Contributor

anntzer commented Oct 14, 2017

Looks like

diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py
index 6cc38bb90..39ac54de1 100644
--- a/lib/matplotlib/axis.py
+++ b/lib/matplotlib/axis.py
@@ -1052,11 +1052,11 @@ class Axis(artist.Artist):
         for tick, loc, label in tick_tups:
             if tick is None:
                 continue
-            if not mtransforms.interval_contains(interval_expanded, loc):
-                continue
             tick.update_position(loc)
             tick.set_label1(label)
             tick.set_label2(label)
+            if not mtransforms.interval_contains(interval_expanded, loc):
+                continue
             ticks_to_draw.append(tick)
 
         return ticks_to_draw

fixes the issue. Basically, the idea is to update the tick labels (and positions) even if they're not going to be drawn (this should be fairly cheap). Feel free to turn the patch into a PR.

I've argued in a few places that we should just be more careful to actually only have the ticks that are going to be drawn, and not have excess ones on either side.

@dstansby dstansby added this to the v2.2 milestone Oct 15, 2017
@NelleV NelleV closed this as completed in 4937314 Oct 17, 2017
@QuLogic QuLogic modified the milestones: needs sorting, v2.2.0 Feb 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants