Skip to content

Instantly share code, notes, and snippets.

@tommylees112
Last active January 27, 2022 11:39
Show Gist options
  • Save tommylees112/821fb3694ab944aef0ad30030df9db7a to your computer and use it in GitHub Desktop.
Save tommylees112/821fb3694ab944aef0ad30030df9db7a to your computer and use it in GitHub Desktop.
Create a list of viridis colors as arrays of `rgba` values
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)
@tommylees112
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment