-
Notifications
You must be signed in to change notification settings - Fork 1
/
color_selection.py
66 lines (57 loc) · 1.71 KB
/
color_selection.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
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
COLORS = mcolors.CSS4_COLORS
DEFAULT_COLORS = [
'indianred',
'coral',
'orange',
'gold',
'lightgreen',
'limegreen',
'mediumturquoise',
'deepskyblue',
'royalblue',
'mediumblue',
'mediumorchid',
'violet',
'hotpink',
'crimson'
]
def get_color(ratio, c_type='default', cmap_type='rainbow'):
# Get color from either DEFAULT_COLORS or Colormap
# ratio: float, ranging from 0 to 1
if c_type == 'default':
return COLORS[DEFAULT_COLORS[int(np.around(ratio * len(DEFAULT_COLORS)))]]
elif c_type == 'cmap':
cmap = plt.get_cmap(cmap_type)
return mcolors.to_hex(cmap(ratio))
else:
raise NotImplementedError('Color type {} is invalid.'.format(c_type))
def show_colors(colors):
x_size = 10
fig, ax = plt.subplots(figsize=(x_size, 1))
ax.yaxis.set_visible(False)
ax.xaxis.set_visible(False)
ax.set_axis_off()
for i, c in enumerate(colors):
x_start = i * (x_size / len(colors))
x_end = (i + 1) * (x_size / len(colors))
ax.hlines(1, x_start, x_end, color=COLORS[c], linewidth=50)
plt.tight_layout()
plt.show()
def show_color_map(cmap_type):
x_size = 10
fig, ax = plt.subplots(figsize=(x_size, 1))
ax.yaxis.set_visible(False)
ax.xaxis.set_visible(False)
ax.set_axis_off()
grad = np.linspace(0, 1, 256)
grad = np.vstack((grad, grad))
ax.imshow(grad, aspect='auto', cmap=plt.get_cmap(cmap_type))
plt.tight_layout()
plt.show()
if __name__ == '__main__':
# For debugging: plot the colors used for the categories
show_colors(DEFAULT_COLORS)
show_color_map('rainbow')