Skip to content

Move away from set_ticklabels() #19016

Closed
Closed
@timhoffm

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions