Skip to content

Instantly share code, notes, and snippets.

@ReneHamburger1993
Forked from dmyersturnbull/fancy_cmaps.py
Created June 20, 2020 19:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ReneHamburger1993/1ffc005bab5c27e95d2a3e670e7c7864 to your computer and use it in GitHub Desktop.
Save ReneHamburger1993/1ffc005bab5c27e95d2a3e670e7c7864 to your computer and use it in GitHub Desktop.
Nice extra colormaps for matplotlib.
class FancyCmaps:
"""
Useful colormaps for matplotlib. Most importantly:
- white_red
- white_blue
- blue_white
- white_black
The built-in matplotlib ones don't actually go between pure color values!!
For ex, 'Greys' doesn't go from pure white to pure black!
So colormaps to consider avoiding include Greys, Blues, Greens, (etc), bwr, and seismic.
Matplotlib still has good built-in colormaps, including viridis and plasma.
"""
@classmethod
def white_red(cls) -> Colormap:
"""A colormap from pure white to pure red."""
cmap = LinearSegmentedColormap.from_list('white_red', ['#ffffff', '#ff0000'])
cmap.set_bad(color='#333333')
return cmap
@classmethod
def white_blue(cls) -> Colormap:
"""A colormap from pure white to pure blue."""
cmap = LinearSegmentedColormap.from_list('white_blue', ['#ffffff', '#0000ff'])
cmap.set_bad(color='#333333')
return cmap
@classmethod
def blue_white(cls) -> Colormap:
"""A colormap from pure white to pure blue."""
cmap = LinearSegmentedColormap.from_list('blue_white', ['#0000ff', '#ffffff'])
cmap.set_bad(color='#333333')
return cmap
@classmethod
def white_black(cls) -> Colormap:
"""A colormap from pure white to pure black."""
cmap = LinearSegmentedColormap.from_list('white_black', ['#ffffff', '#000000'])
cmap.set_bad(color='#dd0000')
return cmap
@classmethod
def blue_white_red(cls) -> Colormap:
"""A colormap from pure blue to pure red."""
cmap = LinearSegmentedColormap.from_list('blue_white_red', ['#0000ff', '#ffffff', '#ff0000'])
cmap.set_bad(color='#aaaaaa')
return cmap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment