-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatplotlib_colormap.py
86 lines (69 loc) · 2.39 KB
/
matplotlib_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
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
"""
Print out (or visualize) samples from a selcted matplotlib colormap.
"""
import argparse
import numpy as np
import matplotlib.cm
parser = argparse.ArgumentParser(description="")
parser.add_argument(
"colormap",
type=str,
help="Colormap name - see: https://matplotlib.org/stable/tutorials/colors/colormaps.html",
)
parser.add_argument(
"--uniform_num",
type=int,
default=10,
help="Number of uniformly sampled values"
)
parser.add_argument(
"--values",
type=float,
nargs="+",
help="List of values in <0,1> interval"
)
parser.add_argument(
"--join_format",
type=str,
choices=["space", "comma", "np"],
default="comma",
help="Number of uniformly sampled values",
)
parser.add_argument("--show", action="store_true", help="Show the colors in CLI")
def main(args):
if args.uniform_num is not None and args.values is None:
args.values = np.linspace(0, 1, args.uniform_num)
color_list = matplotlib.cm.get_cmap(args.colormap)(args.values)[np.newaxis, :, :3]
for row in range(color_list.shape[1]):
color = color_list[0, row, :].flatten()
if args.join_format == "space":
print("{:.3f} {:.3f} {:.3f}".format(color[0], color[1], color[2]))
elif args.join_format == "comma":
print("{:.3f}, {:.3f}, {:.3f}".format(color[0], color[1], color[2]))
elif args.join_format == "np":
if row == 0:
print("[[{:.3f}, {:.3f}, {:.3f}],".format(color[0], color[1], color[2]))
elif row == color_list.shape[1] - 1:
print(" [{:.3f}, {:.3f}, {:.3f}]]".format(color[0], color[1], color[2]))
else:
print(" [{:.3f}, {:.3f}, {:.3f}],".format(color[0], color[1], color[2]))
if args.show:
visualize_colormap(color_list)
def visualize_colormap(color_list):
colormap_text = ""
color_strings = []
colormap_vals = np.linspace(0, 1, 32)
for row in range(color_list.shape[1]):
color = color_list[0, row, :].flatten()
col_i = (255.0 * color).astype(np.uint8)
color_strings.append(
"\x1b[38;2;{};{};{}m".format(col_i[0], col_i[1], col_i[2])
+ "\u2588"
+ "\033[0m"
)
colormap_text = "min " + "".join(color_strings) + " max"
print(colormap_text)
if __name__ == "__main__":
args = parser.parse_args()
main(args)