Skip to content

Move away from set_ticklabels() #19016

Closed
Closed

Description

@timhoffm

This is a placeholder issue so that I don't forget about it. I will some time later implement a draft PR. However, feedback is already welcome.

Problem statement

Axis.set_ticklabels() (and the derived Axes.set_x/yticklabels()) are problematic as they change the text of existing ticks. This relies implicitly on ticks being set previously at adequate positions. Essentially set_ticklabels() can only be used reasonably if set_ticks() has been called before. We try to make users aware of this by a warning in https://matplotlib.org/3.3.3/api/_as_gen/matplotlib.axis.Axis.set_ticklabels.html.

Proposed fix

  1. Add a parameter labels to Axis.set_ticks() so that one can do

    ax.set_xticks([1, 2, 3], labels=['a', 'b', 'c'])
    

    instead of

    ax.set_xticks([1, 2, 3])
    ax.set_xticklabels(['a', 'b', 'c'])
    
  2. Discourage using set_ticklabels(). - I think it's too widely used, so that we cannot deprecate it.

Further comments

This should make set_xticks() more similar/equal to pyplot.xticks(), which already has the labels parameter.

To be sorted out:

  • whether we want to support labels only (set_ticks(labels=['a', 'b', 'c'])
  • the return value of get_xicks(), which only returns the values, whereas pyplot.xticks() without parameters returns ticks, labels. This is a somwhat annoying asymmetry, but probably has to be left as is.
  • some more details.

Activity

  1. added
    API: consistencyConsistency of the matplotlib API, including naming, behavior, defaults, …
    API: changesChanges to the public API, typically requiring deprecation.
    on Nov 26, 2020
  2. mwaskom commented on Nov 28, 2020

    @mwaskom

    Removing ax.set_xticklabels would break ax.set(xticks=..., xticklabels=...), which is very convenient.

  3. timhoffm commented on Nov 28, 2020

    @timhoffm
    MemberAuthor

    @mwaskom Thanks, that's a valid point I did not realize. However, I'm only proposing to discourage set_xticklabels() not remove it.

    1. Discourage using set_ticklabels(). - I think it's too widely used, so that we cannot deprecate it.

    So your example would still work.

  4. story645 commented on Nov 29, 2020

    @story645
    Member

    Though given @mwaskom 's example, maybe ax.set(xticks=(ticks, label)) should work if a goal is to carry through the idea that those go together.

  5. anntzer commented on Dec 6, 2020

    @anntzer
    Contributor

    If we are going to introduce a new API I would suggest perhaps a {tick: label, ...} mapping? That looks the most pythonic to me... Perhaps also support a list of [(tick, label), ...] pairs just in case someone really wants to have two ticks at the same location. The advantage of either format is that they immediately enforce the fact that there should be as many ticks as labels.

  6. timhoffm commented on Dec 12, 2020

    @timhoffm
    MemberAuthor

    I'm buying the structured data type for xticks. it nicely solves the "properties should be one parameter" issue so that we can do ax.set(xticks=zip(ticks, labels)). On the downside we have the asymmetry betweeen set_xticks and get_xticks.

    My main concern is that plt.xticks(positions, labels) and ax.set_xticks(zip(positions, labels)) will have different APIs and I have no good idea how to make that bearable.

  7. added this to the v3.5.0 milestone on Aug 18, 2021
  8. QuLogic commented on Aug 18, 2021

    @QuLogic
    Member

    Closed by #20047, which didn't auto-close, because you have to put the trigger word before every issue reference.

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

    API: changesChanges to the public API, typically requiring deprecation.API: consistencyConsistency of the matplotlib API, including naming, behavior, defaults, …Maintenance

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions