Repository navigation
feat: add register_mpl function to register colormaps with matplotlib - #162
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #162 +/- ##
==========================================
- Coverage 95.91% 95.90% -0.01%
==========================================
Files 168 168
Lines 2252 2273 +21
==========================================
+ Hits 2160 2180 +20
- Misses 92 93 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Hi @tlambert03 thanks for the quick turn around. I think for now this would suffice. We at ultraplot can register colormaps lazily, but matplotlib does not have this feature. I was not aware that the registration was so big on Then this proxy would resolve on load. I think this may mittigate the loading taking long on cmap's end and should be compatible with mpl and ultraplot. |
# Conflicts: # src/cmap/__init__.py
thanks @cvanelteren (and @beskep for the context in Ultraplot/UltraPlot#491).
I'd think I'd rather not register the entire cmap catlog with mpl on import: cmap can end up imported as a dependency of some other library, so an mpl user could have it in their session without knowing, and suddenly see ~1300 extra entries in
plt.colormaps(), the Qt figure options dropdown, etc. Not to mention it would increase cmap import time 100 fold (from ~12 ms to 1 sec). (By contrast, cmasher only registers ~100)So instead, this adds an explicit function:
It only registers fully-qualified namespace:name keys and skips anything already registered. Names are canonical (lowercase, _ not -), so
tol:bright_alt, nottol:bright-alt. I checked it with ultraplot 2.7.1: aftercmap.register_mpl("tol:bright")orcmap.register_mpl("tol:*"),uplt.rc["cmap.qualitative"] = "tol:bright"works (without it, it raises a KeyError).I'd caution against running
cmap.register_mpl("*")at your own import time. If you'd rather not require users to call anything, another option on the UltraPlot side would be a lazy fallback when a key isn't found... something like this (untested sketch) inColormapDatabase.__getitem__:Thoughts? Does this solve your needs? Or are you really hoping for the full cmap catalog to be registered?