Created
October 17, 2019 09:12
-
-
Save elehcim/67db24f3d3511f30e1bca631e5e45417 to your computer and use it in GitHub Desktop.
How to color different plots using a colormap
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 numpy as np | |
import matplotlib | |
import matplotlib.pyplot as plt | |
n_plots = 10 | |
how_many_colors = 12 | |
cmap_name = 'jet' | |
norm = matplotlib.colors.Normalize(vmin=0, vmax=n_plots) | |
cmap = matplotlib.cm.get_cmap(cmap_name, how_many_colors) | |
mappable = plt.cm.ScalarMappable(norm=norm, cmap=cmap) | |
data_list = list() | |
for i in range(n_plots): | |
data_list.append((np.random.rand(5), np.random.rand(5))) | |
fig, ax = plt.subplots() | |
for i in range(n_plots): | |
ax.plot(*data_list[i], color=mappable.to_rgba(i)) | |
fig.colorbar(mappable) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment