This is what I used to make all these.
Last active
November 19, 2019 22:19
-
-
Save endolith/2885984 to your computer and use it in GitHub Desktop.
Show matplotlib colormaps with luminance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This example comes from the Cookbook on www.scipy.org. According to the | |
| # history, Andrew Straw did the conversion from an old page, but it is | |
| # unclear who the original author is. | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from matplotlib import cm | |
| # normal progression | |
| a = np.linspace(0, 1, 256).reshape(1, -1) | |
| # cyclic endpoints | |
| #a = concatenate((np.linspace(0.8, 1, 128,endpoint = False), np.linspace(0, 0.2, 128))).reshape(1,-1) | |
| a = np.vstack((a, a)) | |
| #Luminance (standard, objective): (0.2126*R) + (0.7152*G) + (0.0722*B) | |
| #Luminance (perceived option 1): (0.299*R + 0.587*G + 0.114*B) | |
| #Luminance (perceived option 2, slower to calculate): sqrt( 0.241*R^2 + 0.691*G^2 + 0.068*B^2 ) | |
| def relative_luminance(RGB): | |
| R, G, B = RGB | |
| Y = 0.2126 * R + 0.7152 * G + 0.0722 * B | |
| return Y | |
| # Get a list of the colormaps in matplotlib. Ignore the ones that end with | |
| # '_r' because these are simply reversed versions of ones that don't end | |
| # with '_r' | |
| #maps = [m for m in plt.colormaps() if not m.endswith("_r")] | |
| # sort case insensitive so they're easy to locate | |
| #maps = sorted(maps, str.lower) | |
| # A bunch of dumb attempts at sorting | |
| # Sort by first red value | |
| #maps = sorted(maps, key=lambda a: relative_luminance(cm.cmap_d[a](128)[0:3])) | |
| #maps = sorted(maps, key=lambda a: cm.cmap_d[a](64)[1]) | |
| #maps = sorted(maps, key=lambda a: cm.cmap_d[a](64)[2]) | |
| #maps = sorted(maps, key = lambda name: sum(diff(relative_luminance(cm.cmap_d[name](arange(256)),1)))) | |
| #maps = sorted(maps, key = lambda name: sum(diff([relative_luminance(x) for x in cm.cmap_d[name](arange(256))[:,0:3]]))) | |
| #maps = sorted(maps, key = lambda name: rms_flat(sum(cm.cmap_d[name](arange(256)),1))) | |
| # blue | |
| #maps = sorted(maps, key=lambda a: mean(cm.cmap_d[a](128)[1:])) | |
| #Sorted by hand | |
| maps = [ | |
| ## Qualitative: | |
| # | |
| # 'Accent', | |
| # 'Paired', | |
| # 'Set1', | |
| # 'Dark2', | |
| # 'Set3', | |
| # 'Pastel1', | |
| # 'Pastel2', | |
| # 'Set2', | |
| # | |
| # | |
| ## Maps: | |
| # | |
| # 'ocean', | |
| # 'gist_earth', | |
| # 'terrain', | |
| # 'gist_stern', | |
| # 'brg', | |
| # | |
| ## Decorative: | |
| # | |
| # 'spring', | |
| # 'autumn', | |
| # 'summer', | |
| # 'winter', | |
| # 'cool_r', | |
| # | |
| # | |
| ## Repetitive: | |
| # | |
| # 'flag', | |
| # 'prism', | |
| # | |
| # | |
| ## Spectral: | |
| # | |
| # 'gist_ncar', # erratic non-monotonic luminance | |
| # 'spectral', # erratic non-monotonic luminance | |
| # 'jet', # unimodal but jumpy (discontinouus?) luminance | |
| # 'gist_rainbow_r', # erratic non-monotonic luminance | |
| # 'hsv_r', # erratic non-monotonic luminance, cyclic hue | |
| # 'rainbow', # diverging, smooth unimodal luminance curve | |
| # | |
| # | |
| # | |
| # | |
| ## Diverging: | |
| # 'coolwarm', | |
| # 'RdBu', | |
| # 'seismic', | |
| # 'bwr', | |
| # 'RdYlBu', | |
| # 'Spectral', | |
| # 'rainbow', | |
| # 'BrBG', | |
| # 'PiYG', | |
| # 'PRGn', | |
| # 'PuOr', | |
| # 'RdGy', | |
| # 'RdYlGn', | |
| # | |
| # | |
| # | |
| # | |
| ## Sequential: | |
| # | |
| # 'hot', # jagged luminance, not enough change at extremes, | |
| # 'afmhot', | |
| # 'gist_heat', | |
| # 'copper', | |
| # 'gnuplot', | |
| # 'gnuplot2', | |
| # 'cubehelix', | |
| # 'bone', | |
| # 'gray', | |
| ## 'gist_yarg_r', # redundant | |
| ## 'gist_gray', # redundant | |
| ## 'binary_r', # redundant | |
| 'Greys', | |
| 'pink_r', | |
| 'BuPu', | |
| 'Reds', | |
| 'PuRd', | |
| 'OrRd', | |
| 'YlOrRd', | |
| 'YlOrBr', | |
| 'Oranges', | |
| 'BuGn', | |
| 'Greens', | |
| 'YlGn', | |
| 'PuBuGn', | |
| 'RdPu', | |
| 'Purples', | |
| 'YlGnBu', | |
| 'Blues', | |
| 'PuBu', | |
| 'GnBu', | |
| ] | |
| #maps = sorted(maps, key=lambda a: relative_luminance(cm.cmap_d[a](3)[0:3])) | |
| #maps = sorted(maps, key=lambda a: argmax(cm.cmap_d[a](3)[0:3])) | |
| nmaps = len(maps) # + 1 | |
| fig = plt.figure(figsize=(5, 10)) | |
| fig.subplots_adjust(top=0.99, bottom=0.01, left=0.2, right=0.99) | |
| for i, m in enumerate(maps): | |
| ax = plt.subplot(nmaps, 1, i+1) | |
| # Show a colorbar for each: | |
| # plt.axis("off") | |
| plt.imshow(a, aspect='auto', cmap=plt.get_cmap(m), origin='lower') | |
| # Plot the luminance of each: | |
| plt.setp(ax.get_xticklabels(), visible=False) | |
| plt.setp(ax.get_yticklabels(), visible=False) | |
| plt.setp(ax.get_xticklines(), visible=False) | |
| plt.setp(ax.get_yticklines(), visible=False) | |
| plt.xlim(0, 255) | |
| plt.ylim(0, 1) | |
| plt.plot([relative_luminance(rgb) for rgb in | |
| list(cm.get_cmap(m)(np.arange(256))[:, :3])], color='w') | |
| pos = list(ax.get_position().bounds) | |
| fig.text(pos[0] - 0.01, pos[1], m, fontsize=10, | |
| horizontalalignment='right') | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment