-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolors_from_mpl.py
52 lines (44 loc) · 1.76 KB
/
colors_from_mpl.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
"""
Writes out hex colors from color scales provided in matplotlib
into JS file
python colors_from_mpl.py >> js/colorscales.js
"""
import itertools
import json
import numpy as np
import matplotlib.colors
import matplotlib.cm
# Have colormaps separated into categories:
# http://matplotlib.org/examples/color/colormaps_reference.html
cmap_names = [
('Perceptually Uniform Sequential', [
'viridis', 'plasma', 'inferno', 'magma']),
('Sequential', [
'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn']),
('Sequential (2)', [
'binary', 'gist_yarg', 'gist_gray', 'gray', 'bone', 'pink',
'spring', 'summer', 'autumn', 'winter', 'cool', 'Wistia',
'hot', 'afmhot', 'gist_heat', 'copper']),
('Diverging', [
'PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu',
'RdYlBu', 'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic']),
('Qualitative', [
'Pastel1', 'Pastel2', 'Paired', 'Accent',
'Dark2', 'Set1', 'Set2', 'Set3',
'tab10', 'tab20', 'tab20b', 'tab20c']),
('Miscellaneous', [
'flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern',
'gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg', 'hsv',
'gist_rainbow', 'rainbow', 'jet', 'nipy_spectral', 'gist_ncar'])
]
cm_names = [cat[1] for cat in cmap_names]
print("var mpl_scales = {")
for name in itertools.chain.from_iterable(cm_names):
cmap = matplotlib.cm.get_cmap(name)
values = np.linspace(0, 1, cmap.N)
rgba = cmap(values)
hex = np.apply_along_axis(matplotlib.colors.rgb2hex, axis=1, arr=rgba)
print(' "{}": {},\n'.format(name, json.dumps(hex.tolist())))
print("};")