Skip to content
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

Allow users to register custom matplotlib colormaps #38587

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading