-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fixes pyplot xticks() and yticks() by allowing setting only the labels #15788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
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
Fixes pyplot xticks() and yticks() by allowing setting only the labels #15788
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that four your example, one should rather use plt.boxplot(labels, data)
.
I think there are still cases in which pure label setting is reasonable. Though, I'm slightly concerned that people might abuse this freedom and end up with labels at unexpected positions.
Usually this would need an update of the docstring, but there's #15789 which does already cover that.
This is technically a new feature and needs to be mentioned in the whatsnew. Given that setting labels without positions is already possible with ax.set_xticklabels, I wouldn't worry about exposing it in pyplot as well. |
3035a0a
to
31f332e
Compare
Indeed, my example was not good. I was checking my code that originated this suggestion and noticed that I set xticks through plt because I was plotting inside a for cycle. However, I could've just set the labels inside the for cycle using the arg So, it might be the case that there's no real use case for this change, except for possibly convenience. I did some tests with bar graphs, and this addition can actually lead to unexpected results. Differently from boxplots, bar graphs don't always present one xtick per bar. Thus, setting the By changing To conclude, this change might not be the best solution to this problem. The alternative to this is to return a different error message when setting |
We test if ticks are in the view limit; see #12158 So I guess this function could work to check the current view limit and set the ticks based on what is in view. Maybe the test in #12158 should be factored into a private method so we can use it elsewhere rather than having multiple implementations. But I think that API would be clear enough.... |
That would fix the previous example. However, I don't think it would fix the underlying problem, which is clear in this example: bar plots don't generate a single xtick per bar by default, and thus, setting the xticks without the PS: It seems that the docs generation failed for the added file. I'll fix this asap. A commit squash might be need, apologies. |
There are certainly problems with setting tick labels, but again xticks is basically repro'ing the API of set_xticklabels (and set_xticks) so I don't think this should prevent us from exposing the API at the pyplot level. |
So I guess I’m starting to lean against this PR. It seems rife for confusion to allow the user to specify the labels but not the ticks they expect them attached to. So labels would be optional, but values would not. But I’ve not gone back and parsed all the discussion. |
One can indeed argue that setting labels without ticks is only really needed in rare circumstances and often creates confusion. It would thus also be a valid choice to not support it in the high-level |
Technically even Axes.set_xticklabels wasn't needed, we could just have said "Manually create and set a FixedFormatter", but that was not the design choice taken... |
So instead of this PR, we should probably give a better error message for when the user tries to set the labels without positions (as suggested by timhoffm). Currently it outputs Would this be a preferred solution? |
Put on the agenda of the dev call today. |
I am strongly opposed to allowing labels only, and hence in favor of improving the error message. "Labels-only" is a gun pointed straight at the users foot. |
The consensus is that that we do not want to expose stetting labels only on the pyplot interface. It's hardly needed and a common source of mistakes. Instead, we should error out cleanly. @bluetrickpt Would you like to contribute a PR with a better error message? |
Sure, I can do that either today or tomorrow. I'll probably do on a different PR though, to clean the commit's log. |
PR Summary
This PR was suggested in issue #15005 as to fix the error that is returned when xticks() or yticks() is called with only the labels as parameters.
The changes remove the error by allowing setting only the labels. This is useful for cases where we plot multiple boxplots or bar plots in the same figure, in where we know exactly how many boxplots or bars to plot. The xticks are always range(n), where n is the number of boxplots or bars to put in the plot.
Simple test example:
Note: I didn't add any example to the examples in the function description. Do you think it would be valuable?
PR Checklist