Skip to content

Instantly share code, notes, and snippets.

@pjbull
Created May 29, 2018 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjbull/6aa6b6410497df039efb9663c08dcfef to your computer and use it in GitHub Desktop.
Save pjbull/6aa6b6410497df039efb9663c08dcfef to your computer and use it in GitHub Desktop.
get colors from colormap for arbitrary data
import matplotlib as mpl
import matplotlib.pyplot as plt
# whatever the data is
a = np.arange(5, 15)
# get colors for data from colormap (viridis in this case)
norm = mpl.colors.Normalize(vmin=a.min(), vmax=a.max())
rgba_color = plt.cm.viridis([norm(v) for v in a])
# take a look at the colors
for ci, c in enumerate(rgba_color):
ax = plt.gca()
p = mpl.patches.Rectangle((1*ci, 0), 1, 1, facecolor=c)
ax.add_patch(p)
ax.set_xlim(0, ci + 1)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment