Python 如何在 matplotlib 1.4 中使用 viridis

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32484453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:40:00  来源:igfitidea点击:

How to use viridis in matplotlib 1.4

pythonmatplotlibcolormap

提问by ukrutt

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.pyfrom https://github.com/BIDS/colormap. Unfortunately, I'm not able to make it work. This is what I do:

我想使用颜色图“viridis”(http://bids.github.io/colormap/),我还不会更新到开发版本 1.5。因此,我colormaps.pyhttps://github.com/BIDS/colormap下载。不幸的是,我无法让它工作。这就是我所做的:

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,回溯结束于,

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

ValueError:无法识别颜色图 viridis。可能的值有:光谱、夏季、凉爽、...

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

(然后是最初安装的颜色图的完整列表。)

Any thoughts on how to fix this issue?

关于如何解决这个问题的任何想法?

采纳答案by aganders3

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

要使用 设置viridis为您的颜色图set_cmap,您必须先注册它:

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)

回答by tmdavison

Rather than using set_cmap, which requires a matplotlib.colors.Colormapinstance, you can set the cmapdirectly in the pcolormeshcall

而不是 using set_cmap,这需要一个matplotlib.colors.Colormap实例,您可以cmap直接在pcolormesh调用中设置

(cmaps.viridisis a matplotlib.colors.ListedColormap)

(cmaps.viridismatplotlib.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)

回答by P i

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

来自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)

回答by Han Zhengzu

Download the colormaps.py from here,then:

这里下载 colormaps.py ,然后:

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

Done!

完毕!