|
5 | 5 | from matplotlib.colors import LinearSegmentedColormap, ListedColormap, CSS4_COLORS |
6 | 6 | import random |
7 | 7 | from .utils import _calculate_luminance |
| 8 | +import pandas as pd |
| 9 | +default_cmaps = matplotlib.pyplot.colormaps() |
8 | 10 |
|
9 | 11 | def register_cmap(c): |
10 | | - try: |
11 | | - plt.register_cmap(cmap=c) |
12 | | - except: |
13 | | - matplotlib.colormaps.register(c, force=True) |
| 12 | + try: |
| 13 | + plt.register_cmap(cmap=c) |
| 14 | + except: |
| 15 | + matplotlib.colormaps.register(c, force=True) |
14 | 16 |
|
15 | 17 | def define_cmap(): |
16 | | - all_cmaps = matplotlib.pyplot.colormaps() |
17 | | - # if "binarize" not in all_cmaps: |
18 | | - # c = LinearSegmentedColormap.from_list( |
19 | | - # "binarize", [(0, 'lightgray'), (1, "black")] |
20 | | - # ) |
21 | | - if "exp1" not in all_cmaps: |
22 | | - c = LinearSegmentedColormap.from_list( |
23 | | - "exp1", [(0, "blue"), (0.5, "yellow"), (1, "red")] |
24 | | - ) |
25 | | - register_cmap(c) |
26 | | - if "exp2" not in all_cmaps: |
27 | | - c = LinearSegmentedColormap.from_list( |
28 | | - "exp2", [(0, "#4A5EA0"), (0.5, "#F9FFB2"), (1, "#A42A26")] |
29 | | - ) |
30 | | - register_cmap(c) |
31 | | - if "meth1" not in all_cmaps: |
32 | | - c = LinearSegmentedColormap.from_list( |
33 | | - "meth1", |
34 | | - [ |
35 | | - (0, "#282972"), |
36 | | - (0.2, "#36C4F2"), |
37 | | - (0.4, "#C1D62F"), |
38 | | - (0.6, "#F9AD17"), |
39 | | - (0.8, "#EC3024"), |
40 | | - (1, "#7D1416"), |
41 | | - ], |
42 | | - ) |
43 | | - register_cmap(c) |
44 | | - if "meth2" not in all_cmaps: |
45 | | - c = LinearSegmentedColormap.from_list( |
46 | | - "meth2", |
47 | | - [ |
48 | | - (0, "#4B5DC6"), |
49 | | - (0.1, "#439CFE"), |
50 | | - (0.3, "#55B849"), |
51 | | - (0.5, "gold"), |
52 | | - (0.7, "darkorange"), |
53 | | - (0.9, "tomato"), |
54 | | - (1, "red"), |
55 | | - ], |
56 | | - ) |
57 | | - register_cmap(c) |
58 | | - if "diverging1" not in all_cmaps: |
59 | | - c = LinearSegmentedColormap.from_list( |
60 | | - "diverging1", [(0, "#67a9cf"), (0.5, "#f7f7f7"), (1, "#ef8a62")] |
61 | | - ) |
62 | | - register_cmap(c) |
| 18 | + all_cmaps = matplotlib.pyplot.colormaps() |
| 19 | + # if "binarize" not in all_cmaps: |
| 20 | + # c = LinearSegmentedColormap.from_list( |
| 21 | + # "binarize", [(0, 'lightgray'), (1, "black")] |
| 22 | + # ) |
| 23 | + if "exp1" not in all_cmaps: |
| 24 | + c = LinearSegmentedColormap.from_list( |
| 25 | + "exp1", [(0, "blue"), (0.5, "yellow"), (1, "red")] |
| 26 | + ) |
| 27 | + register_cmap(c) |
| 28 | + if "exp2" not in all_cmaps: |
| 29 | + c = LinearSegmentedColormap.from_list( |
| 30 | + "exp2", [(0, "#4A5EA0"), (0.5, "#F9FFB2"), (1, "#A42A26")] |
| 31 | + ) |
| 32 | + register_cmap(c) |
| 33 | + if "meth1" not in all_cmaps: |
| 34 | + c = LinearSegmentedColormap.from_list( |
| 35 | + "meth1", |
| 36 | + [ |
| 37 | + (0, "#282972"), |
| 38 | + (0.2, "#36C4F2"), |
| 39 | + (0.4, "#C1D62F"), |
| 40 | + (0.6, "#F9AD17"), |
| 41 | + (0.8, "#EC3024"), |
| 42 | + (1, "#7D1416"), |
| 43 | + ], |
| 44 | + ) |
| 45 | + register_cmap(c) |
| 46 | + if "meth2" not in all_cmaps: |
| 47 | + c = LinearSegmentedColormap.from_list( |
| 48 | + "meth2", |
| 49 | + [ |
| 50 | + (0, "#4B5DC6"), |
| 51 | + (0.1, "#439CFE"), |
| 52 | + (0.3, "#55B849"), |
| 53 | + (0.5, "gold"), |
| 54 | + (0.7, "darkorange"), |
| 55 | + (0.9, "tomato"), |
| 56 | + (1, "red"), |
| 57 | + ], |
| 58 | + ) |
| 59 | + register_cmap(c) |
| 60 | + if "diverging1" not in all_cmaps: |
| 61 | + c = LinearSegmentedColormap.from_list( |
| 62 | + "diverging1", [(0, "#67a9cf"), (0.5, "#f7f7f7"), (1, "#ef8a62")] |
| 63 | + ) |
| 64 | + register_cmap(c) |
63 | 65 |
|
64 | | - if "cmap50" not in all_cmaps: |
65 | | - colors = [] |
66 | | - for c in CSS4_COLORS: |
67 | | - l = _calculate_luminance(c) |
68 | | - if l > 0.25 and l < 0.8: |
69 | | - colors.append(c) |
70 | | - c = ListedColormap( |
71 | | - random.sample( |
72 | | - [ |
73 | | - c |
74 | | - for c in CSS4_COLORS.keys() |
75 | | - if c not in ["white", "snow"] and "gray" not in c |
76 | | - ], |
77 | | - 100, |
78 | | - ), |
79 | | - "cmap50", |
80 | | - ) |
81 | | - register_cmap(c) |
| 66 | + if "random50" not in all_cmaps: |
| 67 | + colors = [] |
| 68 | + for c in CSS4_COLORS: |
| 69 | + l = _calculate_luminance(c) |
| 70 | + if l > 0.25 and l < 0.8: |
| 71 | + colors.append(c) |
| 72 | + c = ListedColormap( |
| 73 | + random.sample([c for c in CSS4_COLORS.keys() |
| 74 | + if c not in ["white", "snow"] and "gray" not in c], |
| 75 | + 50), |
| 76 | + "random50", |
| 77 | + ) |
| 78 | + register_cmap(c) |
82 | 79 |
|
83 | | - if "parula" not in all_cmaps: |
84 | | - cm_data = [ |
85 | | - [0.2081, 0.1663, 0.5292], |
86 | | - [0.2116238095, 0.1897809524, 0.5776761905], |
87 | | - [0.212252381, 0.2137714286, 0.6269714286], |
88 | | - [0.2081, 0.2386, 0.6770857143], |
89 | | - [0.1959047619, 0.2644571429, 0.7279], |
90 | | - [0.1707285714, 0.2919380952, 0.779247619], |
91 | | - [0.1252714286, 0.3242428571, 0.8302714286], |
92 | | - [0.0591333333, 0.3598333333, 0.8683333333], |
93 | | - [0.0116952381, 0.3875095238, 0.8819571429], |
94 | | - [0.0059571429, 0.4086142857, 0.8828428571], |
95 | | - [0.0165142857, 0.4266, 0.8786333333], |
96 | | - [0.032852381, 0.4430428571, 0.8719571429], |
97 | | - [0.0498142857, 0.4585714286, 0.8640571429], |
98 | | - [0.0629333333, 0.4736904762, 0.8554380952], |
99 | | - [0.0722666667, 0.4886666667, 0.8467], |
100 | | - [0.0779428571, 0.5039857143, 0.8383714286], |
101 | | - [0.079347619, 0.5200238095, 0.8311809524], |
102 | | - [0.0749428571, 0.5375428571, 0.8262714286], |
103 | | - [0.0640571429, 0.5569857143, 0.8239571429], |
104 | | - [0.0487714286, 0.5772238095, 0.8228285714], |
105 | | - [0.0343428571, 0.5965809524, 0.819852381], |
106 | | - [0.0265, 0.6137, 0.8135], |
107 | | - [0.0238904762, 0.6286619048, 0.8037619048], |
108 | | - [0.0230904762, 0.6417857143, 0.7912666667], |
109 | | - [0.0227714286, 0.6534857143, 0.7767571429], |
110 | | - [0.0266619048, 0.6641952381, 0.7607190476], |
111 | | - [0.0383714286, 0.6742714286, 0.743552381], |
112 | | - [0.0589714286, 0.6837571429, 0.7253857143], |
113 | | - [0.0843, 0.6928333333, 0.7061666667], |
114 | | - [0.1132952381, 0.7015, 0.6858571429], |
115 | | - [0.1452714286, 0.7097571429, 0.6646285714], |
116 | | - [0.1801333333, 0.7176571429, 0.6424333333], |
117 | | - [0.2178285714, 0.7250428571, 0.6192619048], |
118 | | - [0.2586428571, 0.7317142857, 0.5954285714], |
119 | | - [0.3021714286, 0.7376047619, 0.5711857143], |
120 | | - [0.3481666667, 0.7424333333, 0.5472666667], |
121 | | - [0.3952571429, 0.7459, 0.5244428571], |
122 | | - [0.4420095238, 0.7480809524, 0.5033142857], |
123 | | - [0.4871238095, 0.7490619048, 0.4839761905], |
124 | | - [0.5300285714, 0.7491142857, 0.4661142857], |
125 | | - [0.5708571429, 0.7485190476, 0.4493904762], |
126 | | - [0.609852381, 0.7473142857, 0.4336857143], |
127 | | - [0.6473, 0.7456, 0.4188], |
128 | | - [0.6834190476, 0.7434761905, 0.4044333333], |
129 | | - [0.7184095238, 0.7411333333, 0.3904761905], |
130 | | - [0.7524857143, 0.7384, 0.3768142857], |
131 | | - [0.7858428571, 0.7355666667, 0.3632714286], |
132 | | - [0.8185047619, 0.7327333333, 0.3497904762], |
133 | | - [0.8506571429, 0.7299, 0.3360285714], |
134 | | - [0.8824333333, 0.7274333333, 0.3217], |
135 | | - [0.9139333333, 0.7257857143, 0.3062761905], |
136 | | - [0.9449571429, 0.7261142857, 0.2886428571], |
137 | | - [0.9738952381, 0.7313952381, 0.266647619], |
138 | | - [0.9937714286, 0.7454571429, 0.240347619], |
139 | | - [0.9990428571, 0.7653142857, 0.2164142857], |
140 | | - [0.9955333333, 0.7860571429, 0.196652381], |
141 | | - [0.988, 0.8066, 0.1793666667], |
142 | | - [0.9788571429, 0.8271428571, 0.1633142857], |
143 | | - [0.9697, 0.8481380952, 0.147452381], |
144 | | - [0.9625857143, 0.8705142857, 0.1309], |
145 | | - [0.9588714286, 0.8949, 0.1132428571], |
146 | | - [0.9598238095, 0.9218333333, 0.0948380952], |
147 | | - [0.9661, 0.9514428571, 0.0755333333], |
148 | | - [0.9763, 0.9831, 0.0538], |
149 | | - ] |
150 | | - c = LinearSegmentedColormap.from_list("parula", cm_data) |
151 | | - register_cmap(c) |
| 80 | + if "random100" not in all_cmaps: |
| 81 | + colors = [] |
| 82 | + for c in CSS4_COLORS: |
| 83 | + l = _calculate_luminance(c) |
| 84 | + if l > 0.25 and l < 0.8: |
| 85 | + colors.append(c) |
| 86 | + c = ListedColormap( |
| 87 | + random.sample([c for c in CSS4_COLORS.keys() |
| 88 | + if c not in ["white", "snow"] and "gray" not in c], |
| 89 | + 100), |
| 90 | + "random100", |
| 91 | + ) |
| 92 | + register_cmap(c) |
| 93 | + |
| 94 | + if "parula" not in all_cmaps: |
| 95 | + cm_data = [ |
| 96 | + [0.2081, 0.1663, 0.5292], |
| 97 | + [0.2116238095, 0.1897809524, 0.5776761905], |
| 98 | + [0.212252381, 0.2137714286, 0.6269714286], |
| 99 | + [0.2081, 0.2386, 0.6770857143], |
| 100 | + [0.1959047619, 0.2644571429, 0.7279], |
| 101 | + [0.1707285714, 0.2919380952, 0.779247619], |
| 102 | + [0.1252714286, 0.3242428571, 0.8302714286], |
| 103 | + [0.0591333333, 0.3598333333, 0.8683333333], |
| 104 | + [0.0116952381, 0.3875095238, 0.8819571429], |
| 105 | + [0.0059571429, 0.4086142857, 0.8828428571], |
| 106 | + [0.0165142857, 0.4266, 0.8786333333], |
| 107 | + [0.032852381, 0.4430428571, 0.8719571429], |
| 108 | + [0.0498142857, 0.4585714286, 0.8640571429], |
| 109 | + [0.0629333333, 0.4736904762, 0.8554380952], |
| 110 | + [0.0722666667, 0.4886666667, 0.8467], |
| 111 | + [0.0779428571, 0.5039857143, 0.8383714286], |
| 112 | + [0.079347619, 0.5200238095, 0.8311809524], |
| 113 | + [0.0749428571, 0.5375428571, 0.8262714286], |
| 114 | + [0.0640571429, 0.5569857143, 0.8239571429], |
| 115 | + [0.0487714286, 0.5772238095, 0.8228285714], |
| 116 | + [0.0343428571, 0.5965809524, 0.819852381], |
| 117 | + [0.0265, 0.6137, 0.8135], |
| 118 | + [0.0238904762, 0.6286619048, 0.8037619048], |
| 119 | + [0.0230904762, 0.6417857143, 0.7912666667], |
| 120 | + [0.0227714286, 0.6534857143, 0.7767571429], |
| 121 | + [0.0266619048, 0.6641952381, 0.7607190476], |
| 122 | + [0.0383714286, 0.6742714286, 0.743552381], |
| 123 | + [0.0589714286, 0.6837571429, 0.7253857143], |
| 124 | + [0.0843, 0.6928333333, 0.7061666667], |
| 125 | + [0.1132952381, 0.7015, 0.6858571429], |
| 126 | + [0.1452714286, 0.7097571429, 0.6646285714], |
| 127 | + [0.1801333333, 0.7176571429, 0.6424333333], |
| 128 | + [0.2178285714, 0.7250428571, 0.6192619048], |
| 129 | + [0.2586428571, 0.7317142857, 0.5954285714], |
| 130 | + [0.3021714286, 0.7376047619, 0.5711857143], |
| 131 | + [0.3481666667, 0.7424333333, 0.5472666667], |
| 132 | + [0.3952571429, 0.7459, 0.5244428571], |
| 133 | + [0.4420095238, 0.7480809524, 0.5033142857], |
| 134 | + [0.4871238095, 0.7490619048, 0.4839761905], |
| 135 | + [0.5300285714, 0.7491142857, 0.4661142857], |
| 136 | + [0.5708571429, 0.7485190476, 0.4493904762], |
| 137 | + [0.609852381, 0.7473142857, 0.4336857143], |
| 138 | + [0.6473, 0.7456, 0.4188], |
| 139 | + [0.6834190476, 0.7434761905, 0.4044333333], |
| 140 | + [0.7184095238, 0.7411333333, 0.3904761905], |
| 141 | + [0.7524857143, 0.7384, 0.3768142857], |
| 142 | + [0.7858428571, 0.7355666667, 0.3632714286], |
| 143 | + [0.8185047619, 0.7327333333, 0.3497904762], |
| 144 | + [0.8506571429, 0.7299, 0.3360285714], |
| 145 | + [0.8824333333, 0.7274333333, 0.3217], |
| 146 | + [0.9139333333, 0.7257857143, 0.3062761905], |
| 147 | + [0.9449571429, 0.7261142857, 0.2886428571], |
| 148 | + [0.9738952381, 0.7313952381, 0.266647619], |
| 149 | + [0.9937714286, 0.7454571429, 0.240347619], |
| 150 | + [0.9990428571, 0.7653142857, 0.2164142857], |
| 151 | + [0.9955333333, 0.7860571429, 0.196652381], |
| 152 | + [0.988, 0.8066, 0.1793666667], |
| 153 | + [0.9788571429, 0.8271428571, 0.1633142857], |
| 154 | + [0.9697, 0.8481380952, 0.147452381], |
| 155 | + [0.9625857143, 0.8705142857, 0.1309], |
| 156 | + [0.9588714286, 0.8949, 0.1132428571], |
| 157 | + [0.9598238095, 0.9218333333, 0.0948380952], |
| 158 | + [0.9661, 0.9514428571, 0.0755333333], |
| 159 | + [0.9763, 0.9831, 0.0538], |
| 160 | + ] |
| 161 | + c = LinearSegmentedColormap.from_list("parula", cm_data) |
| 162 | + register_cmap(c) |
| 163 | + |
| 164 | +def get_palettable_colors(): |
| 165 | + """ |
| 166 | + category: sequential, diverging, qualitative |
| 167 | + """ |
| 168 | + R=[] |
| 169 | + for category in ['sequential','diverging','qualitative']: |
| 170 | + for source in dir(palettable): |
| 171 | + color_source=getattr(palettable,source) |
| 172 | + if not hasattr(color_source,category): |
| 173 | + continue |
| 174 | + color_category=getattr(color_source,category) |
| 175 | + color_names=[] |
| 176 | + for name in dir(color_category): |
| 177 | + color=getattr(color_category,name) |
| 178 | + if not hasattr(color,'mpl_colors'): |
| 179 | + continue |
| 180 | + R.append([category,color.name,color.type,color.number,color.colors,color.hex_colors,color.mpl_colors,color.mpl_colormap]) |
| 181 | + df=pd.DataFrame(R,columns=['category','name','type','number','colors','hex_colors','mpl_colors','mpl_colormap']) |
| 182 | + df.insert(1,'prefix',df.name.apply(lambda x:x.split('_')[0])) |
| 183 | + df.sort_values(['prefix','number'],ascending=[True,False],inplace=True) |
| 184 | + max_n = df.groupby('prefix').number.max().to_dict() |
| 185 | + df.insert(5, 'max_n', df.prefix.map(max_n)) |
| 186 | + df = df.loc[df.number == df.max_n] |
| 187 | + df['Name'] = df.name.apply(lambda x: x.split('_')[0] if not x.endswith('_r') else x.split('_')[0] + '_r') |
| 188 | + mpl_colormaps = [] |
| 189 | + for cmap, prefix in zip(df.mpl_colormap.tolist(), df.Name.tolist()): |
| 190 | + cmap.name = prefix |
| 191 | + mpl_colormaps.append(cmap) |
| 192 | + df.mpl_colormap = mpl_colormaps |
| 193 | + df = df.loc[~ df.Name.isin(default_cmaps)] |
| 194 | + return df |
| 195 | + |
| 196 | +def register_palettable(): |
| 197 | + df=get_palettable_colors() |
| 198 | + all_cmaps = matplotlib.pyplot.colormaps() |
| 199 | + |
| 200 | + # register qualitative colormap |
| 201 | + df1=df.loc[df.category == 'qualitative'] |
| 202 | + for colors,name in zip (df1.mpl_colors.tolist(),df1.Name.tolist()): |
| 203 | + if name in all_cmaps: |
| 204 | + continue |
| 205 | + c = ListedColormap(colors,name) |
| 206 | + register_cmap(c) |
| 207 | + |
| 208 | + # register sequential and diverging colormap |
| 209 | + df1 = df.loc[df.category != 'qualitative'] |
| 210 | + for colors,name in zip (df1.mpl_colors.tolist(),df1.Name.tolist()): |
| 211 | + if name in all_cmaps: |
| 212 | + continue |
| 213 | + c = LinearSegmentedColormap.from_list(name, colors) |
| 214 | + register_cmap(c) |
152 | 215 |
|
153 | 216 | define_cmap() |
| 217 | +try: |
| 218 | + import palettable |
| 219 | + register_palettable() |
| 220 | +except: |
| 221 | + print("Warning: Please install palettable to enable palettable colormaps") |
| 222 | + |
0 commit comments