Skip to content

Instantly share code, notes, and snippets.

@elehcim
Created October 17, 2019 09:12
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 elehcim/67db24f3d3511f30e1bca631e5e45417 to your computer and use it in GitHub Desktop.
Save elehcim/67db24f3d3511f30e1bca631e5e45417 to your computer and use it in GitHub Desktop.
How to color different plots using a colormap
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