Skip to content

Instantly share code, notes, and snippets.

@wassname
Created September 11, 2025 05:57
Show Gist options
  • Save wassname/b24f09ce60dde0dd13e915a9ee2005f0 to your computer and use it in GitHub Desktop.
Save wassname/b24f09ce60dde0dd13e915a9ee2005f0 to your computer and use it in GitHub Desktop.
matplotlib generator colors from cmap and norm
# How to use matplotlib to assign a spectrum of colors to lines in a plot,
import matplotlib as mpl
strengths = [-2, -1, 0, 2, 3]
v = max(np.abs(strengths))
cnorm = mpl.colors.CenteredNorm(0, v)
cmap = mpl.cm.coolwarm
for s in strengths:
plt.plot(x, y, c=cmap(norm(s))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment