Skip to content

Instantly share code, notes, and snippets.

@thomasaarholt
Created January 26, 2022 13:16
Show Gist options
  • Save thomasaarholt/169a1cf29048dd96c8a6c9b980adb9b6 to your computer and use it in GitHub Desktop.
Save thomasaarholt/169a1cf29048dd96c8a6c9b980adb9b6 to your computer and use it in GitHub Desktop.
Linear colormap from one rgb color to another
from matplotlib.colors import LinearSegmentedColormap
def black_to_color(high_color: tuple, low_color: tuple = (0,0,0), name: str = "my_color", steps=256):
"Get linear colormap from one rgb color to another, defaulting from black"
r1,g1,b1 = low_color
r2,g2,b2 = high_color
cdict = {
'red': [(0.0, r1, r1),
(1.0, r2, r2)],
'green': [(0.0, g1, g1),
(1.0, g2, g2)],
'blue': [(0.0, b1, b1),
(1.0, b2, b2)]}
return LinearSegmentedColormap(name, cdict, N=steps)
blue = (0,0,1) # this is RGB for blue
cmap = black_to_color(blue)
cmap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment