Created
October 10, 2019 02:39
-
-
Save Ken-Kuroki/e626a048907c1f3bb670b932ffee3a79 to your computer and use it in GitHub Desktop.
Get hex format colorcode from a matplotlib colormap
This file contains 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
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