From 2b5375852e2ea5b69980cc020451c7a3f6e19f82 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 15 Nov 2024 23:47:07 +0100 Subject: [PATCH] Don't fail on equal-but-differently-named cmaps in qt figureoptions. Currently, opening the Qt figureoptions UI for an image whose cmap is not registered in the colormap registry, has a name not matching any registry entry, but is actually equal (`==`, i.e. has the same LUT and colorbar-extension attributes) to a registry entry, leads to an error. A typical example would be ``` import cmap, pylab as p # third-party p.imshow([[0, 1]], cmap=cmap.Colormap("bids:magma").to_mpl()) ``` and opening the qt figure options; this leads to the error "index 'bids:magma' is invalid ...". Note that if the cmap is different from any registered cmap then we already add it to the UI combobox (try e.g. `cmap=cmap.Colormap("imagej:fire")`); the only problem was if it was equal to a registered cmap (this arises because when the code was originally written, cmap instance equality was by identity, not by comparing LUTs, so the `cmap not in cm._colormaps.values()` check behaved differently). Fix that by checking whether the colormap *name* is registered. The behavior is still ill-defined in the opposite (theoretical) case of an unregistered cmap different from any registered cmap but with a matching name, but I'd argue that case is more pathological. Test by running the above code and opening the qt figureoptions. --- lib/matplotlib/backends/qt_editor/figureoptions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/backends/qt_editor/figureoptions.py b/lib/matplotlib/backends/qt_editor/figureoptions.py index 2809e63c4e8c..9d31fa9ced2c 100644 --- a/lib/matplotlib/backends/qt_editor/figureoptions.py +++ b/lib/matplotlib/backends/qt_editor/figureoptions.py @@ -149,7 +149,7 @@ def prepare_data(d, init): cmaps = [(cmap, name) for name, cmap in sorted(cm._colormaps.items())] for label, mappable in labeled_mappables: cmap = mappable.get_cmap() - if cmap not in cm._colormaps.values(): + if cmap.name not in cm._colormaps: cmaps = [(cmap, cmap.name), *cmaps] low, high = mappable.get_clim() mappabledata = [