添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
精明的蜡烛  ·  左对齐 Pandas DataFrame ...·  6 月前    · 
高大的楼房  ·  iFrame Error In ...·  8 月前    · 
被表白的洋葱  ·  COUNT - Amazon ...·  10 月前    · 
气宇轩昂的竹笋  ·  [email protected] | ...·  1 年前    · 

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

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

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'])

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.
  • @mwaskom Thanks, that's a valid point I did not realize. However, I'm only proposing to discourage set_xticklabels() not remove it.

  • Discourage using set_ticklabels(). - I think it's too widely used, so that we cannot deprecate it.
  • So your example would still work.

    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.

    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.