Skip to content

Commit e11ef22

Browse files
authored
Merge pull request #67 from bastorer/master
Update cm.py to register maps with matplotlib
2 parents 64bc250 + 22d50cf commit e11ef22

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

cmocean/cm.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import numpy as np
1414
import os
15+
from matplotlib import colors, cm
1516

1617
from . import tools
1718

@@ -34,5 +35,16 @@
3435
cmap_d[cmapname + '_r'] = tools.cmap(rgb[::-1, :], N=256)
3536
cmap_d[cmapname + '_r'].name = cmapname + '_r'
3637

38+
# Register the cmap with matplotlib
39+
rgb_with_alpha = np.zeros((rgb.shape[0],4))
40+
rgb_with_alpha[:,:3] = rgb
41+
rgb_with_alpha[:,3] = 1. #set alpha channel to 1
42+
reg_map = colors.ListedColormap(rgb_with_alpha, 'cmo.' + cmapname, rgb.shape[0])
43+
cm.register_cmap(cmap = reg_map)
44+
45+
# Register the reversed map
46+
reg_map_r = colors.ListedColormap(rgb_with_alpha[::-1,:], 'cmo.' + cmapname + '_r', rgb.shape[0])
47+
cm.register_cmap(cmap = reg_map_r)
48+
3749
# make colormaps available to call
3850
locals().update(cmap_d)

0 commit comments

Comments
 (0)