In a previous post, I used some hard-won knowledge about colormaps to create a lightweight interactive browser for exploring chemical space.
In this post, I’ll share some of the tidbits that learned along the way in that and many other projects and papers.
Discrete colormaps
Colors often represent categorical variables. In such cases, for the majority of people who share a perceptual understanding of color space, it pays to always have colors that look as distinct as possible. (Of course, this is all quite subjective because perceptions of color are in the eye of the beholder; but we aim to produce the maximum effect for a broad audience’s aesthetic sense. )
There are various ways of ensuring this in different color spaces. There is no one-size-fits-all solution to, for instance, complete a circuit in some continuous latent color space and have this map to maximally distinct looking colors.
In pursuit of such a solution, there are many striking color maps which have been painstakingly devised using large numbers of colors for large numbers of categories. Single-cell analysis abounds with such color maps. Here are a few particularly useful and striking ones.
CODE
# Default discrete colormap for <= 20 categories, from https://sashat.me/2017/01/11/list-of-20-simple-distinct-colors/. See also http://phrogz.net/css/distinct-colors.html and http://tools.medialab.sciences-po.fr/iwanthue/cmap_custom_discrete = ["#bdbdbd", '#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#911eb4', '#46f0f0', '#f032e6', '#bcf60c', '#fabebe', '#008080', '#e6beff', '#9a6324', '#fffac8', '#800000', '#aaffc3', '#808000', '#ffd8b1', '#000075', '#808080', '#7d87b9', '#bec1d4', '#d6bcc0']# Convenient discrete colormaps for large numbers of colors.cmap_custom_discrete_44 = ['#745745', '#568F34', '#324C20', '#FF891C', '#C9A997', '#C62026', '#F78F82', '#EF4C1F', '#FACB12', '#C19F70', '#824D18', '#CB7513', '#FBBE92', '#CEA636', '#F9DECF', '#9B645F', '#502888', '#F7F79E', '#007F76', '#00A99D', '#3EE5E1', '#65C8D0', '#3E84AA', '#8CB4CD', '#005579', '#C9EBFB', '#000000', '#959595', '#B51D8D', '#C593BF', '#6853A0', '#E8529A', '#F397C0', '#DECCE3', '#E18256', '#9BAA67', '#8ac28e', '#68926b', '#647A4F', '#CFE289', '#00C609', '#C64B55', '#953840', '#D5D5D5']cmap_custom_discrete_74 = ['#FFFF00', '#1CE6FF', '#FF34FF', '#FF4A46', '#008941', '#006FA6', '#A30059', '#FFDBE5', '#7A4900', '#0000A6', '#63FFAC', '#B79762', '#004D43', '#8FB0FF', '#997D87', '#5A0007', '#809693', '#6A3A4C', '#1B4400', '#4FC601', '#3B5DFF', '#4A3B53', '#FF2F80', '#61615A', '#BA0900', '#6B7900', '#00C2A0', '#FFAA92', '#FF90C9', '#B903AA', '#D16100', '#DDEFFF', '#000035', '#7B4F4B', '#A1C299', '#300018', '#0AA6D8', '#013349', '#00846F', '#372101', '#FFB500', '#C2FFED', '#A079BF', '#CC0744', '#C0B9B2', '#C2FF99', '#001E09', '#00489C', '#6F0062', '#0CBD66', '#EEC3FF', '#456D75', '#B77B68', '#7A87A1', '#788D66', '#885578', '#FAD09F', '#FF8A9A', '#D157A0', '#BEC459', '#456648', '#0086ED', '#886F4C', '#34362D', '#B4A8BD', '#00A6AA', '#452C2C', '#636375', '#A3C8C9', '#FF913F', '#938A81', '#575329', '#00FECF', '#B05B6F']"""Interprets dataset to get list of colors, ordered by corresponding color values."""def get_discrete_cmap(num_colors_needed): cmap_discrete = cmap_custom_discrete_44# If the provided color map has insufficiently many colors, make it cycleiflen(cmap_discrete) < num_colors_needed: cmap_discrete = sns.color_palette(cmap_discrete, num_colors_needed) cmap_discrete = ['#%02x%02x%02x'% (int(255*red), int(255*green), int(255*blue)) for (red, green, blue) in cmap_discrete]return cmap_discrete
Continuous colormaps
There are a couple of different kinds of continuous color maps, and it is essential to use the correct one for the correct type of data.
Perceptually uniform colormaps
Perceptually uniform color maps tend to work best with quantile normalization or percentile normalization. This is natural because the two distributions of the data and of perceptual space are being aligned. This point bears reemphasizing.
Diverging and sequential colormaps
Sequential and diverging color maps are well known to apply to different types of continuous data. Data also need to be appropriately normalized before being passed into such color maps, which is a separate discussion.
But diverging colormaps in particular often look unsightly and hide certain dynamic ranges of the data within perceptual line spots. So a few favorite diverging colormaps which are old standbys are available here.