Last active
January 27, 2022 11:39
-
-
Save tommylees112/821fb3694ab944aef0ad30030df9db7a to your computer and use it in GitHub Desktop.
Create a list of viridis colors as arrays of `rgba` values
This file contains hidden or 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
from matplotlib import cm | |
from matplotlib.colors import ListedColormap, LinearSegmentedColormap | |
import seaborn as sns | |
n_unique_vals = 5 | |
colors = cm.get_cmap('viridis', n_unique_vals)(np.linspace(0, 1, n_unique_vals)) | |
sns.palplot(colors) | |
# create colormap and mappable for colorbar construction | |
cmap = LinearSegmentedColormap.from_list("RANDOM", colors, N=n_pcs) | |
sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(vmin=0, vmax=n_unique_vals)) | |
plt.colorbar(sm, ticks=np.arange(n_unique_vals) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another colorbar question: https://stackoverflow.com/questions/8342549/matplotlib-add-colorbar-to-a-sequence-of-line-plots