-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Implementing ticklabels keyword in plt.colorbar() #18884
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
Conversation
What is the use case for explicitly setting labels? I mainly see categoricals, but that does not make sense for a colorbar. |
@timhoffm From my view, it is not us to decide if something makes sense or not. I will still provide some examples for you: May it be that someone wants to visualize the certainty of a neural network that something is part of a category. Or a user is looking for an easy/hacky way of adding units behind the tick numbers. There even is a demo for this, with the following approach: cbar = fig.colorbar(cax, ticks=[-1, 0, 1])
cbar.ax.set_yticklabels(['< -1', '0', '> 1']) Not only is this complicated, but also frustrating for users. Remember: Most of your users are no experts and, for example, set ticks with |
If we are adding API then we need to decide if it makes sense or not. OTOH this seems pretty useful to me. Imagine a pcolor of phase and wanting to label \pi/2, \pi etc. |
I'm a bit hesitant with adding functionality for explicit labels, because it tends to be a can of worms: The ticks positions and labels must be synchronized. If you just pass labels they will end up on the automatically chosen ticks, hence their position is dependent on the data(range) - That e.g. is explicitly forbidden in IMHO colorbar_tick_labelling_demo.html should be changed because the <1 / >1 don't actually make sense. But @jklymak's phase example seems like a fair use case, which is why one can consider a Concerning frustration: I see your point that it's more cumbersome to work on the axes afterwards as in the example. If one can provide a consistent simplification, that's a benefit for this use case. However the frustration is even larger if a claimed functionality does not work consistently. With a |
Just to be clear, the last couple of draft PRs I have put in are attempting to make colorbar axes the same as normal axes as much as is practical. Its a bit far off, maybe even after 3.4, but it is a goal. Then most of the tricks that work for normal axes should carry over. |
@timhoffm plt.colorbar(ticks=None, ticklabels=['foo', 'bar'])
**output:** UserWarning: set_ticks() must have been called. I believe that addresses your main concern. Regarding your second argument: Would it be possible to call |
I think as long as
I am not sure what you are suggesting here. The way that ticks and tick labels get decided at draw time is via Locators and Formatters. All that should happen when you call |
@jklymak My second thought was related to Tim's concern, that directly setting
I guess that is exactly what my PR is doing. I tried to utilize as much of existing code as possible, and indeed the kwarg ticklabels is handed over to set_ticklabels(ticklabels) which then creates an FixedFormatter. |
To clarify: I'm conceptually ok with adding a
I've not completely thought this through, so there may be more. @jklymak Will there still be public attributes and a |
I agree with all the above. For sure
For sure. Nothing I'm working on will change the API (much, I hope). Its low level with how we draw the solids and the extend triangles. OTOH, I'm all for cleaning up the API. The changes I am making may make more API easier. ie. |
@timhoffm @jklymak Please have a look at the latest commit. Now, ticks are only accepted with they are |
Something is broken here. Tests are failing with |
@timhoffm I have investigated the issue and found the bug. All existing tests are now passing again. I have also implemented additional tests for the new ticklables keyword. Can you explain, why the last test of mine fails? Why doesn't the error match?
(I have aligned the warnings so one can see that the strings are exactly the same) |
The match parameter is interpreted as a regular expression, so you have to escape the brackets. |
@timhoffm Thank you, that worked. Can you also point me in the right direction regarding the failed build of the documentation? I do not find a meaningful error message. |
From CI log:
|
@timhoffm Thank you tim, it's all fixed now. |
@jklymak please review |
'length as the ticks.') | ||
# check if objects in list have valid type | ||
elif not all(type(item) in [str, float, int] for item in ticklabels): | ||
cbook._warn_external('ticklabels need to be a list of str') |
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.
It would be good to check these warnings and the next one if you have time for that....
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.
Actually, w/o reading the discussion, do we need all these warning checks? I don't think we usually check for mismatched type, for instance...
Given #19016 (comment), I'm wondering if we should accept a dict/list of tuples for the |
OK, sorry for letting this languish. This should just pass through to |
@danuo, I'm going to close this, because how colorbar axes are handled is pretty different now that #20054 is in. If you wanted to refactor based on master, that would be great. However, you should also simply be able to do Thanks for your work on this! |
PR Summary
This adds the ticklabels keyword to the plt.colorbar() function. Tests and documentation has been added. Please review the documentation, as I am unsure if this has been done correctly.
PR Checklist
pytest
passes).flake8
on changed files to check).flake8-docstrings
and runflake8 --docstring-convention=all
).doc/users/next_whats_new/
(follow instructions in README.rst there).doc/api/next_api_changes/
(follow instructions in README.rst there).