-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeriMap.py
58 lines (42 loc) · 1.95 KB
/
GeriMap.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
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import numpy as np
def get(reverse=False):
gm = [(0, 0, 0), (.15, .15, .5), (.3, .15, .75),
(.6, .2, .50), (1, .25, .15), (.9, .5, 0),
(.9, .75, .1), (.9, .9, .5), (1, 1, 1)]
if reverse:
return LinearSegmentedColormap.from_list('GeriMap_r', gm[::-1])
else:
return LinearSegmentedColormap.from_list('GeriMap', gm)
def registerGeriMap(transparencyThreshold=0.4):
"""
Register the perceptually uniform colormap 'GeriMap' with matplotlib
transparencyThreshold: All intensities below this threshold will have
a non-zero alpha value (making them more or
less transparent). The opacity varies linearly,
being 1 at this value, and 0 at zero.
"""
gm = [(0, 0, 0), (.15, .15, .5), (.3, .15, .75),
(.6, .2, .50), (1, .25, .15), (.9, .5, 0),
(.9, .75, .1), (.9, .9, .5), (1, 1, 1)]
gerimap = LinearSegmentedColormap.from_list('GeriMap', gm)
gerimap_r = LinearSegmentedColormap.from_list('GeriMap_r', gm[::-1])
if transparencyThreshold is not None:
n = int(gerimap.N * transparencyThreshold)
nn = gerimap.N - n
if n < 0 or nn < 0:
raise ValueError('Transparency threshold must be a value between 0 and 1.')
a = np.linspace(0, 1, n)
b = np.ones((nn,))
gmap = gerimap(np.arange(gerimap.N))
gmap[:,-1] = np.concatenate((a,b), axis=None)
gerimap = LinearSegmentedColormap.from_list('GeriMap', gmap)
gmap_r = gerimap_r(np.arange(gerimap.N))
gmap_r[:,-1] = np.concatenate((a,b), axis=None)
gerimap_r = LinearSegmentedColormap.from_list('GeriMap_r', gmap_r)
cmaps = plt.colormaps()
if gerimap.name not in cmaps:
plt.register_cmap(cmap=gerimap)
plt.register_cmap(cmap=gerimap_r)
return gerimap, gerimap_r