You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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.
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.
Activity
mwaskom commentedon Nov 28, 2020
Removing
ax.set_xticklabelswould breakax.set(xticks=..., xticklabels=...), which is very convenient.timhoffm commentedon Nov 28, 2020
@mwaskom Thanks, that's a valid point I did not realize. However, I'm only proposing to discourage
set_xticklabels()not remove it.So your example would still work.
story645 commentedon Nov 29, 2020
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.anntzer commentedon Dec 6, 2020
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.timhoffm commentedon Dec 12, 2020
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 betweeenset_xticksandget_xticks.My main concern is that
plt.xticks(positions, labels)andax.set_xticks(zip(positions, labels))will have different APIs and I have no good idea how to make that bearable.QuLogic commentedon Aug 18, 2021
Closed by #20047, which didn't auto-close, because you have to put the trigger word before every issue reference.