简体   繁体   English

如何在matplotlib中为热图使用预制的颜色贴图?

[英]How can I use a pre-made color map for my heat map in matplotlib?

I want to use a color map from http://goo.gl/5P4CT for my matplotlib heat map. 我想使用http://goo.gl/5P4CT的彩色地图作为我的matplotlib热图。

I tried doing this: 我试过这样做:

myHeatMap.imshow(heatMap, extent=ext, cmap=get_cmap(cm.datad["Spectral"]))

However, the Python interpreter complains 然而,Python解释器抱怨

in get_cmap
if name in cmap_d:
    TypeError: unhashable type: 'dict'

What is the proper way to use one of these color maps? 使用这些彩色地图的正确方法是什么?

It looks like you are simply calling get_cmap wrong. 看起来你只是在调用get_cmap错误。 Try: 尝试:

from pylab import imshow, show, get_cmap
from numpy import random

Z = random.random((50,50))   # Test data

imshow(Z, cmap=get_cmap("Spectral"), interpolation='nearest')
show()

在此输入图像描述

What are the named colormaps? 什么是命名的色彩映射?

Running the code: 运行代码:

from pylab import cm
print cm.datad.keys()

Gives a list of colormaps, any of which can be substituted for "Spectral" : 给出一个彩色图列表,其中任何一个都可以代替"Spectral"

['Spectral', 'summer', 'RdBu', 'Set1', 'Set2', 'Set3', 'brg_r', 'Dark2', 'hot', 'PuOr_r', 'afmhot_r', 'terrain_r', 'PuBuGn_r', 'RdPu', 'gist_ncar_r', 'gist_yarg_r', 'Dark2_r', 'YlGnBu', 'RdYlBu', 'hot_r', 'gist_rainbow_r', 'gist_stern', 'gnuplot_r', 'cool_r', 'cool', 'gray', 'copper_r', 'Greens_r', 'GnBu', 'gist_ncar', 'spring_r', 'gist_rainbow', 'RdYlBu_r', 'gist_heat_r', 'OrRd_r', 'bone', 'gist_stern_r', 'RdYlGn', 'Pastel2_r', 'spring', 'terrain', 'YlOrRd_r', 'Set2_r', 'winter_r', 'PuBu', 'RdGy_r', 'spectral', 'flag_r', 'jet_r', 'RdPu_r', 'Purples_r', 'gist_yarg', 'BuGn', 'Paired_r', 'hsv_r', 'bwr', 'YlOrRd', 'Greens', 'PRGn', 'gist_heat', 'spectral_r', 'Paired', 'hsv', 'Oranges_r', 'prism_r', 'Pastel2', 'Pastel1_r', 'Pastel1', 'gray_r', 'PuRd_r', 'Spectral_r', 'gnuplot2_r', 'BuPu', 'YlGnBu_r', 'copper', 'gist_earth_r', 'Set3_r', 'OrRd', 'PuBu_r', 'ocean_r', 'brg', 'gnuplot2', 'jet', 'bone_r', 'gist_earth', 'Oranges', 'RdYlGn_r', 'PiYG', 'YlGn', 'binary_r', 'gist_gray_r', 'Accent', 'BuPu_r', 'gist_gray', 'flag', 'seismic_r', 'RdBu_r', 'BrBG', 'Reds', 'BuGn_r', 'summer_r', 'GnBu_r', 'BrBG_r', 'Reds_r', 'RdGy', 'PuRd', 'Accent_r', 'Blues', 'Greys', 'autumn', 'PRGn_r', 'Greys_r', 'pink', 'binary', 'winter', 'gnuplot', 'pink_r', 'prism', 'YlOrBr', 'rainbow_r', 'rainbow', 'PiYG_r', 'YlGn_r', 'Blues_r', 'YlOrBr_r', 'seismic', 'Purples', 'bwr_r', 'autumn_r', 'ocean', 'Set1_r', 'PuOr', 'PuBuGn', 'afmhot']

When plotting with matplotlib you can use cmap=plt.get_cmap('name_of_colormap') For example: plt.pcolormesh(ter_x,ter_y,masked_height.data,cmap=plt.get_cmap('terrain')) 使用matplotlib绘图时,您可以使用cmap=plt.get_cmap('name_of_colormap')例如: plt.pcolormesh(ter_x,ter_y,masked_height.data,cmap=plt.get_cmap('terrain'))

There are many pre-defined names, all of which are listed here . 有许多预定义的名称,所有这些都列在这里

However, I find it difficult to imagine what a 2d plot might look like just by looking at a colorbar. 但是,我发现仅仅通过查看一个颜色条就很难想象一个2D图可能是什么样子。 So I created a terrain map with each possible matplotlib colormap that you can look at here . 所以我创建了一个地形图,其中包含每个可能的matplotlib色彩图,您可以在此处查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM