Skip to content

Setting xticklabels causes warning related to FixedFormatter #18848

Closed

Description

@igurin-invn

Bug report

Bug summary

I get a warning related to FixedFormatter even though I am not setting a formatter.

Thanks to user Rational-IM on StackOverflow and the good folks at pandas for isolating and reproducing the bug.

Code for reproduction

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.scatter(0.5, 0.5)
ax.set_xticklabels(['0', r'$T/2$', r'$T$'])

# Optional line
ax.xaxis.set_major_locator(FixedLocator([0, 0.5, 1]))

None

Actual outcome

<ipython-input-3-52a3699e4278>:5: UserWarning: FixedFormatter should only be used together with FixedLocator
  ax.set_xticklabels(['0', r'$T/2$', r'$T$'])

Expected outcome

Works on 3.2.2 with no warning.

Matplotlib version

  • Operating system: Win 10
  • Matplotlib version: 3.3.2
  • Matplotlib backend: module://ipykernel.pylab.backend_inline
  • Python version: 3.8.5
  • Jupyter version (if applicable): 6.1.4
  • Other libraries: none

Installed from conda

Activity

  1. jklymak commented on Oct 30, 2020

    @jklymak
    Member

    That's on purpose. You should also explicitly specify the three ticks to go with the label and then this warning should go away.

    OTOH I agree the warning is pretty mysterious if u are just using set_xticklabels.

  2. igurin-invn commented on Oct 30, 2020

    @igurin-invn
    ContributorAuthor

    I see. It would be good to fix the message.

    Also, If you do set the tick locations, you have to do it first (I revised my code sample to show this). Yes, it's cleaner to set the locations first and then the labels, but I don't think it should be a warning. As long as the locator and labels have both been set by the time the plot is drawn, it should be OK.

  3. jklymak commented on Oct 30, 2020

    @jklymak
    Member

    The problem is when people zoom or otherwise change the view, and we continue to use an AutoLocator, the labels will often end up applied to the wrong ticks. By encouraging you to manually specify the ticks we help you avoid that possibility.

  4. timhoffm commented on Oct 31, 2020

    @timhoffm
    Member

    Possibly adding a warning a

    if isinstance(locator, mticker.FixedLocator):

    makes sense.

  5. Rational-IM commented on Nov 1, 2020

    @Rational-IM

    Thanks, @jklymak: now I understand why the warning was included. However, I'm assuming there is a lot of code out there - the one I use the most on a day-to-day basis included - that only plots "fixed" charts. Therefore, having to add a line of code using FixedLocator (if what I want is just to format the existing labels), would make the conde unnecessarily polluted, no? Maybe the potential issue you describe should be included somewhere on matplotlib's documentation?

  6. jklymak commented on Nov 1, 2020

    @jklymak
    Member

    Its a warning that you are doing something poorly defined. If you want to suppress it either specify the ticks

    ax.set_xticks([1, 2, 3])
    ax.set_xticklabels(['$\pi$', '$2\pi$', '$3\pi$']) 

    filter the Warning, or just ignore it.

  7. timhoffm commented on Nov 1, 2020

    @timhoffm
    Member

    Side note: I'm tempted to modify set_ticks()
    from set_ticks(ticks, *, minor=False)
    to set_ticks(ticks, *, labels=None, minor=False).

    That would allow to set_ticks([1, 2, 3], labels=['$\pi$', '$2\pi$', '$3\pi$']) and would exactly prevent this problem. A bit hesitant because that'd break the symmetry between get/set. OTOH it does not make sense to set labels without controlling positions, and that's most of the time best done by setting both simultaneously.

    Note also that pyplot.xticks() does something siimilar, though it fully merges set_ticks and set_ticklabels`; and I don't think we can mimic this on the Axes due to backward-compatibility (whether that'd be a good idea would need to be evaluated separately..

  8. hrr2020 commented on Apr 10, 2021

    @hrr2020

    This worked fine in a similar situation:

    ax.set_xticks(ax.get_xticks())  # just get and reset whatever you already have
    ax.set_xticklabels(xtick_labels)  # set the new/modified labels
    
  9. anntzer commented on Apr 14, 2021

    @anntzer
    Contributor

    fwiw I agree with @timhoffm's proposal at #18848 (comment), although a variant (that I think I suggested somewhere else) may be to support ax.set_xticks({pos: label, pos: label, ...}).

  10. jklymak commented on Apr 14, 2021

    @jklymak
    Member

    I would find a dict hard to remember and assemble. But two matching lists seems fine to me...

  11. timhoffm commented on Apr 14, 2021

    @timhoffm
    Member

    A single parameter would work relatively well also if you start with two lists, either as dict

    set_xticks(dict(zip(positions, labels)))
    

    and/or as list of tuples

    set_xticks(zip(positions, labels))
    

    My concern is however, that we basically don't have such compound parameters in our API and mostly use separate parameters.

  12. igurin-invn commented on Apr 15, 2021

    @igurin-invn
    ContributorAuthor

    I would probably assemble such a dict using a dict comprehension.

  13. timhoffm commented on Apr 15, 2021

    @timhoffm
    Member

    In case we should accept a dict, you are free to create that any way you want.

    On a side node, note however, that for a dict comprehension {k: v for k, v in zip(positions, labels)}, you'll need to create the k, v pairs anyway using zip(...). dict(zip(...)) is shorter.

  14. anntzer commented on Apr 15, 2021

    @anntzer
    Contributor

    I'm not overly wedded to the dict idea either, but it's likely that the absence of such APIs in our codebase just reflects our matlab heritage.

  15. igurin-invn commented on Apr 15, 2021

    @igurin-invn
    ContributorAuthor

    @timhoffm , I disagree. {tick: f(tick) for tick in positions} would be very convenient.

    Boo Matlab!

  16. 1 remaining item

  17. amueller commented on May 22, 2021

    @amueller
    Contributor

    Hm can someone summarize the suggested solution after #20047?
    Basically I want to do string formatting of the form ax.set_xticklabels([f(t) for t in ax.get_xticklabels()]). I tried using a FuncFormatter, but couldn't get it to work, presumably because I'm using pandas which does something under the hood.

    Example:

    import pandas as pd
    from matplotlib.ticker import FuncFormatter
    
    pd.Series({'a': 10, 'b': 20, 'c': 30}).plot.bar()
    ax = plt.gca()
    formatter = FuncFormatter(lambda x, pos: f"{x}, {pos}")
    ax.xaxis.set_major_formatter(formatter)

    image
    I would have expected x to be a, b and c but it's not.

    What I had before was something like

    pd.Series({'a': 10, 'b': 20, 'c': 30}).plot.bar()
    ax = plt.gca()
    ax.set_xticklabels([f"{x.get_text()}_is_great" for x in ax.get_xticklabels()])
    

    image

  18. added a commit that references this issue on May 22, 2021
  19. QuLogic commented on May 22, 2021

    @QuLogic
    Member

    Those are categorical plots; the axis is not the categories, but 0, 1, 2, 3... which is transformed by StrCategoryFormatter into the correct labels. You will probably want to grab the existing formatter, wrap its result, and return that back. Something like:

    import matplotlib.ticker as mticker
    
    
    class SpecialFormatter(mticker.Formatter):
        def __init__(self, original_formatter):
            self.original_formatter = original_formatter
    
        def __call__(self, x, pos=None):
            x = self.original_formatter(x, pos=pos)
            return f"{x}, {pos}"
    
    
    ax.xaxis.set_major_formatter(SpecialFormatter(ax.xaxis.get_major_formatter()))
    
  20. amueller commented on May 22, 2021

    @amueller
    Contributor

    Thanks, I figured that the category-indexes were transformed by a formatter.

    I feel that requires a lot more knowledge of the internals than what I was doing before, and it's quite a common operation.
    Basically if you want to apply a function to the ticks in an existing plot, this is what you have to do. Was that the suggested solution before as well?

    Basically this is a DerivedFuncFormatter that allows me to apply a function to the existing formatter, so it's a bit more complicated than what @jklymak suggested above. For a library this works as a recipe, for a notebook it's not very convenient.

  21. jklymak commented on May 22, 2021

    @jklymak
    Member

    Sorry if I'm not following, what is wrong with the following? It seems to work fine with categoricals?

    ax.set_xticklabels([f"{x.get_text()}_is_great" for x in ax.get_xticklabels()])
    
  22. Olshansk commented on Aug 20, 2021

    @Olshansk

    If you're like me and were simply trying to rotate the axis labels without any other changes, the following answer on stack overflow avoids the warning: https://stackoverflow.com/a/52461208/768439.

  23. tdpetrou commented on Dec 7, 2021

    @tdpetrou
    Contributor

    I ran into the same issue as @amueller - attempting to use FuncFormatter on tick labels that were internally stored as categories. You have to have deep knowledge of Matplotlib to understand what is going on as the labels appear as strings in the plot but are stored as numeric internally.

    The easiest solution seems to be:

    ax.set_xticklabels([my_func(x.get_text()) for x in ax.get_xticklabels()])
  24. jklymak commented on Dec 7, 2021

    @jklymak
    Member

    That seems orthogonal to this issue, and has always been the case - FuncFormatter always worked on the tick values, not the label text.

  25. story645 commented on Dec 7, 2021

    @story645
    Member

    FuncFormatter always worked on the tick values, not the label text.

    The values for categorical should be the strings, but I can't think of a good way to do implement that given the internal mpl architecture needs it to be numeric.

  26. added a commit that references this issue on Jan 11, 2022
  27. added a commit that references this issue on Oct 21, 2022
  28. added a commit that references this issue on Feb 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions