Permalink
Browse files

Merge pull request #71 from nextstrain/default-colors-2

Move default color ramp from Viridis to Nextstrain rainbow
  • Loading branch information...
trvrb committed Nov 14, 2017
2 parents 16e0080 + 937b097 commit 338ee576c31dbf5094762349e03e9e60ca9fbc06
Showing with 29 additions and 12 deletions.
  1. +10 −10 base/utils.py
  2. +19 −2 scripts/swap_colors.py
View
@@ -11,17 +11,17 @@ def generate_cmap(data, discrete):
returns dict of (e.g.) country -> hex
'''
norm = mpl.colors.Normalize(0, len(data) - 1)
cmap = mpl.cm.get_cmap("viridis")
if discrete:
if len(data) <= 10:
cmap = mpl.cm.get_cmap("Vega10")
elif len(data) <= 20:
cmap = mpl.cm.get_cmap("Vega20")
ret = []
for idx, val in enumerate(list(data)):
ret.append((val, mpl.colors.to_hex(cmap(norm(idx)))))
return ret
nbins = len(data)
else:
nbins = 256
default_colors = ["#511EA8", "#482BB6", "#4039C3", "#3F4ACA", "#3E5CD0", "#416CCE", "#447CCD", "#4989C4", "#4E96BC", "#559FB0", "#5DA8A4", "#66AE96", "#6FB388", "#7AB77C", "#85BA6F", "#91BC64", "#9DBE5A", "#AABD53", "#B6BD4B", "#C2BA46", "#CDB642", "#D6B03F", "#DDA83C", "#E29D39", "#E69036", "#E67F33", "#E56D30", "#E2592C", "#DF4428", "#DC2F24"]
cmap = mpl.colors.LinearSegmentedColormap.from_list('default_cmap', default_colors, N=nbins)
colors = [(val, mpl.colors.to_hex(cmap(norm(idx)))) for idx, val in enumerate(list(data))]
return colors
def define_latitude_longitude(lat_long_defs, log):
import csv
View
@@ -23,6 +23,9 @@
custom_colors = [l.strip() for l in open(custom_colors, 'r').readlines()] # [categorycolor1, categorycolor2,...]
custom_colors = [split('[\s,\\t]', c, maxsplit=1) for c in custom_colors] # [ [category, color], ...]
custom_colors = { l[0] : l[1] for l in custom_colors} # { category: color }
else:
custom_colors = None
default_colors = [
[],
@@ -58,6 +61,8 @@
["#511EA8", "#482BB6", "#4039C3", "#3F4ACA", "#3E5CD0", "#416CCE", "#447CCD", "#4989C4", "#4E96BC", "#559FB0", "#5DA8A4", "#66AE96", "#6FB388", "#7AB77C", "#85BA6F", "#91BC64", "#9DBE5A", "#AABD53", "#B6BD4B", "#C2BA46", "#CDB642", "#D6B03F", "#DDA83C", "#E29D39", "#E69036", "#E67F33", "#E56D30", "#E2592C", "#DF4428", "#DC2F24"]
]
def swap_colors(json_file_path):
'''
Switches out color ramp in meta.json files.
@@ -70,10 +75,22 @@ def swap_colors(json_file_path):
for k,v in color_options.items():
if 'color_map' in v:
categories, colors = zip(*v['color_map'])
try: ## Uses custom colors if provided AND present for all categories in the dataset
## Use custom colors if provided AND present for all categories in the dataset
if custom_colors and all([category in custom_colors for category in categories]):
colors = [ custom_colors[category] for category in categories ]
except: ## Falls back to default nextstrain colors
## Expand the color palette if we have too many categories
elif len(categories) > len(default_colors):
from matplotlib.colors import LinearSegmentedColormap, to_hex
from numpy import linspace
expanded_cmap = LinearSegmentedColormap.from_list('expanded_cmap', default_colors[-1], N=len(categories))
discrete_colors = [expanded_cmap(i) for i in linspace(0,1,len(categories))]
colors = [to_hex(c).upper() for c in discrete_colors]
else: ## Falls back to default nextstrain colors
colors = default_colors[len(categories)] # based on how many categories are present; keeps original ordering
j['color_options'][k]['color_map'] = map(list, zip(categories, colors))
json.dump(j, open(json_file_path, 'w'), indent=1)

0 comments on commit 338ee57

Please sign in to comment.