Skip to content

Instantly share code, notes, and snippets.

@kaedonkers
Last active December 11, 2018 10:06
Show Gist options
  • Save kaedonkers/c8b2aebb5a74ef4d03f61442a1faae20 to your computer and use it in GitHub Desktop.
Save kaedonkers/c8b2aebb5a74ef4d03f61442a1faae20 to your computer and use it in GitHub Desktop.
Matplotlib: Custom cmap for controuf using ListedColormap
"""
Example courtesy of @pp-mo
Use ListedColormap with matplotlib.pyplot.contourf to speficy colour of each filled layer.
More information can be found at:
https://matplotlib.org/1.5.3/api/colors_api.html#matplotlib.colors.ListedColormap
"""
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
data = np.arange(12.).reshape((3,4))
cmap = ListedColormap(('red', 'black', 'yellow', 'magenta'))
n_levels = cmap.N - 1
plt.contourf(data, n_levels, cmap=cmap)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment