diff --git a/docs/source/release/v6.12.0/Workbench/Bugfixes/38587.rst b/docs/source/release/v6.12.0/Workbench/Bugfixes/38587.rst new file mode 100644 index 000000000000..71c748c1d4ee --- /dev/null +++ b/docs/source/release/v6.12.0/Workbench/Bugfixes/38587.rst @@ -0,0 +1 @@ +- The colorbar (used in Sliceviewer) has been updated to again allow the registration of custom matplotlib colormaps in scripts. diff --git a/qt/python/mantidqt/mantidqt/widgets/colorbar/colorbar.py b/qt/python/mantidqt/mantidqt/widgets/colorbar/colorbar.py index 520e470a1fc3..fa8a667f5f14 100644 --- a/qt/python/mantidqt/mantidqt/widgets/colorbar/colorbar.py +++ b/qt/python/mantidqt/mantidqt/widgets/colorbar/colorbar.py @@ -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) @@ -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) diff --git a/qt/python/mantidqt/mantidqt/widgets/colorbar/test/test_colorbar.py b/qt/python/mantidqt/mantidqt/widgets/colorbar/test/test_colorbar.py index a1b23acb3729..5d26e5efc274 100644 --- a/qt/python/mantidqt/mantidqt/widgets/colorbar/test/test_colorbar.py +++ b/qt/python/mantidqt/mantidqt/widgets/colorbar/test/test_colorbar.py @@ -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 @@ -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)