-
Notifications
You must be signed in to change notification settings - Fork 4
/
check_mp_colors.py
executable file
·174 lines (135 loc) · 4.75 KB
/
check_mp_colors.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python3
## Pombert lab, 2024
version = '0.2c'
updated = '2024-05-27'
name = 'check_mp_colors.py'
import argparse
from matplotlib import colormaps
from pylab import *
from numpy import outer
################################################################################
## README
################################################################################
usage = f"""
NAME {name}
VERSION {version}
UPDATED {updated}
SYNOPSIS Checks available matplotlib + seaborn color palettes on the system
REQS matplotlib, seaborn
COMMAND {name} \\
--list \\
--plot \\
--outfile palettes.png
OPTIONS:
-l (--list) List available color palettes
-p (--plot) Plot available color palettes
-o (--outfile) Output file [Default: color_palettes.png]
-h (--height) Figure height in inches [Default: 10.8]
-w (--width) Figure width in inches [Default: 19.2]
-f (--font) Font size [Default: 6]
-c (--check) Check if color palettes entered exists
--version Show script version
"""
# Print custom message if argv is empty
if (len(sys.argv) <= 1):
print(usage)
exit(0)
################################################################################
## Create command lines switches
################################################################################
cmd = argparse.ArgumentParser(add_help=False)
cmd.add_argument("-l", "--list", action='store_true')
cmd.add_argument("-p", "--plot", action='store_true')
cmd.add_argument("-o", "--outfile", default='color_palettes.png')
cmd.add_argument("-w", "--width", default=60)
cmd.add_argument("-h", "--height", default=6)
cmd.add_argument("-f", "--font", default=12)
cmd.add_argument("-c", "--check", nargs='*')
cmd.add_argument("--version", action='store_true')
args = cmd.parse_args()
colorlist = args.list
colorplot = args.plot
outfile = args.outfile
height = args.height
width = args.width
fontsize = args.font
check = args.check
scversion = args.version
#########################################################################
### Version
#########################################################################
if scversion:
print ("")
print (f"Script: {name}")
print (f"Version: {version}")
print (f"Updated: {updated}\n")
exit(0)
################################################################################
## Check for available color palettes
################################################################################
if colorlist:
print(f"\n##### Available color palettes #####\n")
## matplotlib
mcolors = list(sorted(colormaps))
numcol = len(mcolors)
## matplotlib + seaborn
import seaborn as sns
matsea = list(sorted(colormaps))
catcol = len(matsea)
## seaborn only
seaborn = []
for x in matsea:
if x not in mcolors:
seaborn.append(x)
seacol = len(seaborn)
## print color info
print(f"# matplotlib + seaborn ({catcol}):\n")
print(', '.join(matsea), "\n")
print(f"# matplotlib only ({numcol}):\n")
print(', '.join(mcolors), "\n")
print(f"# seaborn only ({seacol}):\n")
print(', '.join(seaborn), "\n")
################################################################################
## Perform check for colors then exit
################################################################################
if check:
import seaborn as sns
mcolors = list(sorted(colormaps))
false_colors = []
dcolors = {}
for palette in mcolors:
dcolors[palette] = 1
for palette in check:
if palette in dcolors:
next
else:
false_colors.append(palette)
if len(false_colors) >= 1:
print("\n[E] The following color palettes are not available:", *false_colors)
print(f"\nAvailable matplotlib + seaborn color palettes ({len(mcolors)}):\n")
print(', '.join(mcolors), "\n")
exit()
if len(false_colors) == 0:
print("\n[OK] All requested color palettes are available:", *check, "\n")
exit()
################################################################################
## Plot available color palettes
################################################################################
import seaborn as sns
mcolors = list(sorted(colormaps))
plt.rcParams["figure.figsize"] = (width,height)
if colorplot:
a = outer(arange(0,1,0.01),ones(10))
subplots_adjust(
top=0.9,
bottom=0.05,
left=0.01,
right=0.99
)
palette_num = len(mcolors) + 1
for x, name in enumerate(mcolors):
subplot(1, palette_num, x + 1)
axis("off")
imshow(a, aspect='equal', cmap=get_cmap(name), origin='lower')
title(name, rotation=90, fontsize=fontsize)
plt.savefig(outfile)