[英]How can I find out to which category a matplotlib colormap belongs?
Matplotlib 将他们的颜色图分为不同的类别,如“顺序”、“定性”等。 (参见matplotlib 文档)
从给定的matplotlib.colors.ListedColormap
object 中,是否有一种简单(或有点简单)的方法可以找出它是否被归类为“定性”?
在 Colormap object 本身我找不到任何类别信息。
据我所知,此信息未存储在颜色映射对象中(我查看了cm.py
和_cm.py
)。
如果你喜欢你链接到的教程中的分类,你可以把它变成一本字典:
cmap_categories = {
'Perceptually Uniform Sequential':
['viridis', 'plasma', 'inferno', 'magma', 'cividis'],
'Sequential':
['Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn'],
'Sequential (2)':
['binary', 'gist_yarg', 'gist_gray', 'gray', 'bone',
'pink', 'spring', 'summer', 'autumn', 'winter', 'cool',
'Wistia', 'hot', 'afmhot', 'gist_heat', 'copper'],
'Diverging':
['PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu', 'RdYlBu',
'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic'],
'Cyclic':
['twilight', 'twilight_shifted', 'hsv'],
'Qualitative':
['Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2',
'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b',
'tab20c'],
'Miscellaneous':
['flag', 'prism', 'ocean', 'gist_earth', 'terrain',
'gist_stern', 'gnuplot', 'gnuplot2', 'CMRmap',
'cubehelix', 'brg', 'gist_rainbow', 'rainbow', 'jet',
'turbo', 'nipy_spectral', 'gist_ncar']
}
然后你可以通过翻转键和值从中创建一个查找表:
cmap_lookup = {v: k for k, vs in cmap_categories.items() for v in vs}
一个 Matplotlib 颜色图至少知道它自己的名字,所以你可以这样查找一个类别:
from matplotlib import cm
cmap = cm.viridis
category = cmap_lookup.get(cmap.name)
请注意, ListedColormap
并不存储所有颜色图; 它提供了一种制作您自己的颜色图的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.