-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Create a hidden colorbar on-the-fly to format imshow cursor data if no colorbar exists yet. #12473
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
Why don’t you just add a formatter to the artist? This API May be preferable anyways to using the colorbar formatter though that could be the default. |
Because the correct Formatter depends on the Normalization subclass being used, and that logic currently only exists in the colorbar code (nowhere else does it say that LogNorm should use LogFormatter, etc.). |
Can’t we refactor that somehow? Seems quite hacky that you have to create a colorbar to make this work. |
The relationship between norms, scales, colorbars, locators and formatters has been discussed to death (although in a slightly deconstructed fashion) a few times e.g. in #7294 (#7294 (comment), #7294 (comment), ...); TLDR: I think Norms and Scales should be the same thing. |
I think the relevant code for matching up the Norm and Formatter is just: matplotlib/lib/matplotlib/colorbar.py Lines 395 to 402 in f77c979
Can't we just make |
No, you also need to have an underlying axes that sets the locs for the formatter (that's |
You are right, but that seems quite unfortunate. I think most places where How does this work with lognorms anyways? The log formatter returns empty strings a lot of the time. |
Oooh, does |
Didn't have time to look at the code, but I agree with @jklymak that this sounds extremely hacky. If it's not simple to refactor: What happens if we do not implement this? Does something crash or do we just not have the output displayed? |
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
fmt = mticker.ScalarFormatter()
fmt.create_dummy_axis()
fmt.set_locs([0.01, 1000])
print('Scalar:', fmt(20))
fmt = mticker.LogFormatterSciNotation()
fmt.create_dummy_axis()
fmt.set_locs([0.01, 1000])
print('10:', fmt(10))
print('20:', fmt(20))
print('100:', fmt(100))
print('200:', fmt(200)) Gives:
As above, not sure you even ever want to LogFormatter, but maybe I'm misunderstanding how this works... |
@jklymak Actually, you need to call fmt.axis.set_view_interval (instead of fmt.set_locs).
gives
(I guess I should add a @timhoffm Then we just fallback on the old label string (which is much less useful): #12459 protects everything under a |
f33913f
to
56a5cc9
Compare
I still think we should create a dummy axis, not colorbar... |
Perhaps(??) things will become easier after the followup PRs after format_ticks get handled. |
56a5cc9
to
f8463d7
Compare
... if no colorbar exists yet.
f8463d7
to
af18238
Compare
Superseded by #20949. |
PR Summary
Followup to #12459, which also works when no colorbar exists. Adds a second commit on top of #12459.
Made as a separate PR as it's a bit an ugly hack (optimally we'd factor out the formatter selection) but still proposing it as I think the behavior is quite nice.
I don't think it'd be a performance issue as we don't actually draw() the hidden figure -- it doesn't even have a renderer attached to it.
PR Checklist