Python 如何在matplotlib 1.4中使用viridis
我想使用colormap“viridis”(),我还不会更新到开发版本1.5。因此,我从下载了Python 如何在matplotlib 1.4中使用viridis,python,matplotlib,colormap,Python,Matplotlib,Colormap,我想使用colormap“viridis”(),我还不会更新到开发版本1.5。因此,我从下载了colormaps.py。不幸的是,我不能让它工作。我就是这么做的: 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
colormaps.py
。不幸的是,我不能让它工作。我就是这么做的:
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)
这给了我一个ValueError
,回溯以
ValueError:无法识别Colormap viridis。可能的值有:光谱、夏季、凉爽、温暖
(然后是最初安装的彩色地图的完整列表。)
关于如何解决此问题有何想法?您可以直接在
pcolormesh
调用中设置cmap
而不是使用set\u cmap
,这需要matplotlib.colors.Colormap
实例
(cmap.viridis
是一个matplotlib.colors.ListedColormap
)
要使用
set\u cmap
将viridis
设置为您的颜色映射,您必须首先注册它:
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)
我所做的只是复制
_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 matplotlib.colors import ListedColormap
viridis = ListedColormap(_viridis_data, name='viridis')
plt.register_cmap(name='viridis', cmap=viridis)
plt.set_cmap(viridis)
从下载colormaps.py,然后:
import os,sys
scriptpath = "/Your downloading path/colormap-master/"
sys.path.append(os.path.abspath(scriptpath))
import colormaps as cmaps
完成了 太棒了,正是我所需要的。什么会导致导入恐怖:没有名为colormaps的模块?@Annan:你从下载了colormaps.py吗?@tom谢谢你回答我的问题。我没有意识到这是一个单独的模块(我一直在问一些关于“viridis”cmap的问题,没有完全阅读您回答的问题)。实际上,我通过将matplotlib更新为最新版本(对我来说是1.5.3)解决了这个问题。是的,显然获得
viridis
的最佳方法是使用最新版本的matplotlib
(即任何版本的>=1.5
)。然而,这个问题和答案是关于将viridis
与matplotlib(1.4)版本一起使用的,该版本不包括viridis colormap
import os,sys
scriptpath = "/Your downloading path/colormap-master/"
sys.path.append(os.path.abspath(scriptpath))
import colormaps as cmaps