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
How to use viridis in matplotlib 1.4
提问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.py
from 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.py
从https://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 viridis
as 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.Colormap
instance, you can set the cmap
directly in the pcolormesh
call
而不是 using set_cmap
,这需要一个matplotlib.colors.Colormap
实例,您可以cmap
直接在pcolormesh
调用中设置
(cmaps.viridis
is a matplotlib.colors.ListedColormap
)
(cmaps.viridis
是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)
回答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)