Skip to content

Instantly share code, notes, and snippets.

@campenr
Created March 20, 2017 19:40
Show Gist options
  • Save campenr/eff85d6192b273fef5ee8cdbdf74224c to your computer and use it in GitHub Desktop.
Save campenr/eff85d6192b273fef5ee8cdbdf74224c to your computer and use it in GitHub Desktop.
Recreating the pre version 2.0 matplotlib qualitative colormaps
"""
Richard Campen, 2017
Before matplotlib version 2.0 the set of qualitative colormaps 'Accent', 'Dark2', 'Paired', 'Pastel1', 'Pastel2', 'Set1',
'Set2', and 'Set3' were continuous colormaps. As of matplotlib 2.0 these colormaps are discrete as per the change logs
(http://matplotlib.org/users/whats_new.html#qualitative-colormaps).
To recreate the continuous form of these colormaps as per pre 2.0 use the below code, changing the name of the colormap
as necessary.
"""
from pylab import imshow, rand, colorbar
from matplotlib.cm import get_cmap
from matplotlib.colors import LinearSegmentedColormap
current_cmap = get_cmap('Set1')
cmap_colors = [[rgb for rgb in color] for color in current_cmap.colors]
new_cmap = LinearSegmentedColormap.from_list('new_cmap', cmap_colors)
# # Run the below to visualise the new colorbar in a jupyter notebook
# %matplotlib inline
# imshow(rand(18,20), cmap=new_cmap, vmin=0, vmax=1, interpolation="nearest")
# colorbar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment