Skip to content

Instantly share code, notes, and snippets.

@Ken-Kuroki
Created October 10, 2019 02:39
Show Gist options
  • Save Ken-Kuroki/e626a048907c1f3bb670b932ffee3a79 to your computer and use it in GitHub Desktop.
Save Ken-Kuroki/e626a048907c1f3bb670b932ffee3a79 to your computer and use it in GitHub Desktop.
Get hex format colorcode from a matplotlib colormap
def get_colorcode(colormap, value):
"""
Gets hex format colorcode (eg. '#ff0000') from a matplotlib colormap.
Parameters
----------
colormap : matplotlib.colors.Colormap
Matplotlib colormap, a subclass of Colormap
value : float
Value in [0, 1) interval that specifies the color
Returns
-------
colorcode: str
Hex-formatted colorcode string
"""
return "#"+"".join([hex(each)[2:].zfill(2) for each in colormap(value, bytes=True)][:-1])
# how to use
get_colorcode(cm.jet, 0.2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment