19

I want to use the colormap "viridis" (http://bids.github.io/colormap/), and I won't be updating to the development version 1.5 quite yet. Thus, I have downloaded colormaps.py from https://github.com/BIDS/colormap. Unfortunately, I'm not able to make it work. This is what I do:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

import colormaps as cmaps

img=mpimg.imread('stinkbug.png')
lum_img = np.flipud(img[:,:,0])

plt.set_cmap(cmaps.viridis)
imgplot = plt.pcolormesh(lum_img)

This gives me a ValueError, the traceback ending with,

ValueError: Colormap viridis is not recognized. Possible values are: Spectral, summer, coolwarm, ...

(And then the complete list of originally installed colormaps.)

Any thoughts on how to fix this issue?

4 Answers 4

16

To set viridis as your colormap using set_cmap, you must register it first:

import colormaps as cmaps
plt.register_cmap(name='viridis', cmap=cmaps.viridis)
plt.set_cmap(cmaps.viridis)

img=mpimg.imread('stinkbug.png')
lum_img = np.flipud(img[:,:,0])
imgplot = plt.pcolormesh(lum_img)
1
  • Awesome, just what I need.
    – ukrutt
    Sep 9, 2015 at 16:58
12

Rather than using set_cmap, which requires a matplotlib.colors.Colormap instance, you can set the cmap directly in the pcolormesh call

(cmaps.viridis is a matplotlib.colors.ListedColormap)

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

import colormaps as cmaps

img=mpimg.imread('stinkbug.png')
lum_img = np.flipud(img[:,:,0])

imgplot = plt.pcolormesh(lum_img, cmap=cmaps.viridis)
4
  • What would cause ImportError: No module named colormaps ?
    – AnnanFay
    Oct 10, 2016 at 11:35
  • @Annan: Have you downloaded colormaps.py from github.com/BIDS/colormap?
    – tmdavison
    Oct 10, 2016 at 11:58
  • @tom Thanks for replying to my query. I didn't realise it was a separate module (I've been jumping around a few questions about the 'viridis' cmap and did not fully read the question you were answering). I actually fixed the issue by updating matplotlib to the latest version - which for me is 1.5.3.
    – AnnanFay
    Oct 10, 2016 at 12:09
  • @ Annan. Yes, obviously the best way to get viridis is to use the latest version of matplotlib (i.e. any version >= 1.5). However, this question and answer were about using viridis with a version of matplotlib (1.4) which did not include the viridis colormap
    – tmdavison
    Oct 10, 2016 at 12:11
3

What I did is to just copy the

_viridis_data = [[0.267004, 0.004874, 0.329415],
                 [0.268510, 0.009605, 0.335427],
                 [0.269944, 0.014625, 0.341379],
                 :
                 [0.983868, 0.904867, 0.136897],
                 [0.993248, 0.906157, 0.143936]]

from https://github.com/BIDS/colormap/blob/master/colormaps.py

and add:

from matplotlib.colors import ListedColormap

viridis = ListedColormap(_viridis_data, name='viridis')

plt.register_cmap(name='viridis', cmap=viridis)
plt.set_cmap(viridis)
2

Download the colormaps.py from here,then:

import os,sys
scriptpath = "/Your downloading path/colormap-master/"
sys.path.append(os.path.abspath(scriptpath))
import colormaps as cmaps   

Done!

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.