Skip to content

Instantly share code, notes, and snippets.

@jdbcode
Last active April 11, 2024 20:09
Show Gist options
  • Save jdbcode/33d37999f950a36b43e058d15280b536 to your computer and use it in GitHub Desktop.
Save jdbcode/33d37999f950a36b43e058d15280b536 to your computer and use it in GitHub Desktop.
Get a list of n hex colors for a given matplotlib palette
# Source: https://stackoverflow.com/a/33597599/5391200
from pylab import *
cmap = cm.get_cmap('seismic', 5) # matplotlib color palette name, n colors
for i in range(cmap.N):
rgb = cmap(i)[:3] # will return rgba, we take only first 3 so we get rgb
print(matplotlib.colors.rgb2hex(rgb))
cmap = cm.get_cmap('plasma', 101)
color_list = [matplotlib.colors.rgb2hex(cmap(i)[:3]) for i in range(cmap.N)]
print(color_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment