Created
May 29, 2018 23:59
-
-
Save pjbull/6aa6b6410497df039efb9663c08dcfef to your computer and use it in GitHub Desktop.
get colors from colormap for arbitrary data
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
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