Skip to content

Instantly share code, notes, and snippets.

@charlottenosam
Last active April 17, 2019 13:15
Show Gist options
  • Save charlottenosam/c63b6caa68bd117e35a4c6a7ca98007d to your computer and use it in GitHub Desktop.
Save charlottenosam/c63b6caa68bd117e35a4c6a7ca98007d to your computer and use it in GitHub Desktop.
Plot lines with color defined by a parameter
import matplotlib as mpl
import matplotlib.pyplot as plt
# Normalize the array vals so they can be mapped to a color
c_norm = mpl.colors.Normalize(vmin=np.min(array), vmax=np.max(array))
# Pick a colormap
c_map = mpl.cm.viridis
# Scalar mappable of normalized array to colormap
s_map = mpl.cm.ScalarMappable(cmap=c_map, norm=c_norm)
s_map.set_array([])
# Plot the lines
for value in array:
plt.plot(x, y_func(val), color=s_map.to_rgba(value))
# Colorbar
plt.colorbar(s_map, label=r'Array Values')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment