Skip to content

Commit a222272

Browse files
committed
Add magic syntax for running viscm on any importable matplotlib cm
Example: python -m viscm view colorcet:m_fire
1 parent 6b75d84 commit a222272

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

viscm/gui.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,29 @@ def _vis_axes(fig):
174174
return axes
175175

176176

177+
def lookup_colormap_by_name(name):
178+
try:
179+
return plt.get_cmap(name)
180+
except ValueError:
181+
pass
182+
# Try expanding a setuptools-style entrypoint:
183+
# foo.bar:baz.quux
184+
# -> import foo.bar; return foo.bar.baz.quux
185+
if ":" in name:
186+
module_name, object_name = name.split(":", 1)
187+
object_path = object_name.split(".")
188+
import importlib
189+
cm = importlib.import_module(module_name)
190+
for entry in object_path:
191+
cm = getattr(cm, entry)
192+
return cm
193+
raise ValueError("Can't find colormap {!r}".format(name))
194+
177195
class viscm(object):
178196
def __init__(self, cm, figure=None, uniform_space="CAM02-UCS",
179197
name=None, N=256, N_dots=50, show_gamut=False):
180198
if isinstance(cm, str):
181-
cm = plt.get_cmap(cm)
199+
cm = lookup_colormap_by_name(cm)
182200
if name is None:
183201
name = cm.name
184202
if figure == None:
@@ -926,7 +944,7 @@ def load(self, path):
926944
sys.exit("Unsupported filetype")
927945
else:
928946
self.can_edit = False
929-
self.cmap = plt.get_cmap(path)
947+
self.cmap = lookup_colormap_by_name(path)
930948
self.name = path
931949

932950

0 commit comments

Comments
 (0)