-
Notifications
You must be signed in to change notification settings - Fork 1
/
colormap.py
74 lines (59 loc) · 2.36 KB
/
colormap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Code and tools related to colormaps
# Written by R. Thedin
# To use the code below:
# sys.path.append(os.path.abspath('/home/rthedin/utilities/'))
# from colormap import createColormap
import matplotlib
import matplotlib.colors as mcolors
import numpy as np
# Register a RdGr colormap
reds = matplotlib.colormaps['Reds_r'].resampled(128)
greens = matplotlib.colormaps['Greens'].resampled(128)
redgreen = np.vstack((reds(np.linspace(0, 1, 128)), greens(np.linspace(0, 1, 128))))
RdGr = mcolors.ListedColormap(redgreen, name='RdGr')
RdGr_r = RdGr.reversed(name='RdGr_r')
# Register colormaps
try:
matplotlib.colormaps.register(cmap=RdGr)
matplotlib.colormaps.register(cmap=RdGr_r)
except:
pass
# Create a sequential colormap with every CSS4 color, named <color>s.
# Colors available at https://matplotlib.org/stable/gallery/color/named_colors.html
for c in mcolors.CSS4_COLORS:
# Create colormap with white beginning
cmap = mcolors.LinearSegmentedColormap.from_list(name=f'{c}s', colors=['white',c])
cmap_r = RdGr.reversed(name=f'{c}s_r')
# Register colormaps
matplotlib.colormaps.register(cmap=cmap)
matplotlib.colormaps.register(cmap=cmap_r)
def createColormap(colors):
'''
Registers new colormap and its reversed form.
Each color is of the same length on the final map.
Example call:
-------------
createColormap(['firebrick','white','darkgreen'])
The map "firebrickwhitedarkgreen" is now available and is
the same as RdGr above.
Input:
------
colors: array of str
Named colors (CSS4_COLORS) to create the colormap
'''
if isinstance(colors, str):
raise ValueError (f'Colors should be a list of 2 or more colors')
cmapstr = ''.join(colors)
# Create colormap with white beginning
cmap = mcolors.LinearSegmentedColormap.from_list(name=cmapstr, colors=colors)
cmap_r = RdGr.reversed(name=f'{cmapstr}_r')
# Register colormaps
try:
matplotlib.colormaps.register(cmap=cmap)
matplotlib.colormaps.register(cmap=cmap_r)
print(f'Colormap called "{cmapstr}" and "{cmapstr}_r" registered.')
except ValueError as e:
if str(e) == f'A colormap named "{cmapstr}" is already registered.':
print(f'Colormap "{cmapstr}" and "{cmapstr}_r" already registered. Skipping.')
else:
raise