-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_cmap.py
executable file
·43 lines (36 loc) · 1.46 KB
/
custom_cmap.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
import matplotlib.colors as colors
import numpy as np
# TODO
#Make it scale properly
#How does matplotlib
#scaling work
def custom_cmap(colormaps, lower, upper, log = 0):
'''
colormaps : a list of N matplotlib colormap classes
lower : the lower limits for each colormap: array or tuple
upper : the upper limits for each colormap: array or tuple
log : Do you want to plot logscale. This will create
a color map that is usable with LogNorm()
'''
if log == 1:
upper = [np.log10(i/lower[0]) for i in upper]
lower = [np.log10(i/lower[0]) for i in lower]
norm = upper[-1:][0]
else:
lower = lower
upper = upper
norm = upper[-1:][0]
cdict = { 'red':[], 'green':[],'blue':[] }
for color in ['red','green','blue']:
for j,col in enumerate(colormaps):
#print j,col.name,color
x = [i[0] for i in col._segmentdata[color]]
y1 = [i[1] for i in col._segmentdata[color]]
y0 = [i[2] for i in col._segmentdata[color]]
x = [(i-min(x))/(max(x)-min(x)) for i in x]
x = [((i * (upper[j] - lower[j]))+lower[j])/norm for i in x]
if (j == 0) & (x[0] != 0):
x[:0],y1[:0],y0[:0] = [0],[y1[0]],[y0[0]]
for i in range(len(x)): #first x needs to be zero
cdict[color].append((x[i],y1[i],y0[i]))
return colors.LinearSegmentedColormap('my_cmap',cdict)