Skip to content

Commit

Permalink
Merge pull request #38587 from rosswhitfield/sliceviewer_allow_custom…
Browse files Browse the repository at this point in the history
…_colormaps

Allow users to register custom matplotlib colormaps
  • Loading branch information
SilkeSchomann authored Jan 13, 2025
2 parents a411fec + ac35505 commit e9176f3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/source/release/v6.12.0/Workbench/Bugfixes/38587.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- The colorbar (used in Sliceviewer) has been updated to again allow the registration of custom matplotlib colormaps in scripts.
13 changes: 9 additions & 4 deletions qt/python/mantidqt/mantidqt/widgets/colorbar/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ class ColorbarWidget(QWidget):
scaleNormChanged = Signal()
# register additional color maps from file
register_customized_colormaps()
# create the list
cmap_list = sorted([cmap for cmap in colormaps.keys() if not cmap.endswith("_r")])

def __init__(self, parent=None, default_norm_scale=None):
"""
:param default_scale: None uses linear, else either a string or tuple(string, other arguments), e.g. tuple('Power', exponent)
"""

super(ColorbarWidget, self).__init__(parent)
super().__init__(parent)

# create the list. Initialize in the init so that it can be updated if new colormaps are added
self.cmap_list = sorted([cmap for cmap in colormaps.keys() if not cmap.endswith("_r")])

self.setWindowTitle("Colorbar")
self.setMaximumWidth(100)
Expand Down Expand Up @@ -181,7 +182,11 @@ def set_mappable(self, mappable):
self.colorbar = Colorbar(ax=self.ax, mappable=mappable)
self.cmin_value, self.cmax_value = mappable.get_clim()
self.update_clim_text()
self.cmap_changed(cmap, False)
try:
self.cmap_changed(cmap, False)
except ValueError:
# the default mantid colormap is not available, just use matplotlib default
pass

mappable_cmap = get_current_cmap(mappable)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from mantidqt.utils.qt.testing import start_qapplication
from mantidqt.widgets.colorbar.colorbar import ColorbarWidget, NORM_OPTS
from matplotlib.colors import LogNorm, Normalize, SymLogNorm
from matplotlib.colors import LogNorm, Normalize, SymLogNorm, ListedColormap


@start_qapplication
Expand Down Expand Up @@ -239,3 +239,18 @@ def test_invalid_cmin_syntax_is_reset(self):
self.widget.clim_changed()

self.assertEqual("0.0", self.widget.cmin.text())

def test_custom_colormaps(self):
# test that users can register custom colormaps and have it as an option in the colorbar
cmap_name = "custom_cmap"
self.assertFalse(cmap_name in plt.colormaps)
self.assertTrue(self.widget.cmap.findText(cmap_name) == -1)

plt.colormaps.register(cmap=ListedColormap([[0, 0, 0], [0, 0, 1]]), name=cmap_name)
self.assertTrue(cmap_name in plt.colormaps)

# now when you create a colorbar widget, the custom colormap should now be available
cb = ColorbarWidget()
self.assertTrue(cb.cmap.findText(cmap_name) != -1)

plt.colormaps.unregister(cmap_name)

0 comments on commit e9176f3

Please sign in to comment.