Python如何查看并打印matplotlib中所有的colormap(cmap)类型

 更新时间:2022年11月03日 11:33:49   作者:勤奋的大熊猫  
这篇文章主要介绍了Python如何查看并打印matplotlib中所有的colormap(cmap)类型,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

查看并打印matplotlib中所有的colormap(cmap)类型

代码如下:

方法一

import matplotlib.pyplot as plt

cmaps = sorted(m for m in plt.cm.datad if not m.endswith("_r"))
print(cmaps)

我们忽略以_r结尾的类型,因为它们都是类型后面不带有_r的反转版本(reversed version)。

所有的类型我们可以在matplotlib的源代码中找到:(如下图)

方法二

import matplotlib.pyplot as plt

cmap_list1 = plt.colormaps()
print(cmap_list1)

方法三

如果使用的是Pycharm编译器,那么可以在作图的时候简单的随便给定一个cmap的类型,如果给定的cmap类型是错误的,那么在编译器的错误提示信息中也会显示出所有的cmap类型。

比如,我们这里我们想要做一个高斯函数的曲面分布图,我们随意给cmap一个'aaa'的值,这时,我们可以在编译器提示窗口看到如下错误信息的输出。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
x, y = np.meshgrid(x, y)
w0 = 1
gaussian = np.exp(-((pow(x, 2) + pow(y, 2)) / pow(w0, 2)))

fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(x, y, gaussian, cmap='aaa')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
"""
错误提示信息:
ValueError: 'aaa' is not a valid value for name; supported values are 'Accent',
 'Accent_r', 'Blues', 'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', 'BuGn_r', 'BuPu', 
 'BuPu_r', 'CMRmap', 'CMRmap_r', 'Dark2', 'Dark2_r', 'GnBu', 'GnBu_r', 'Greens',
 'Greens_r', 'Greys', 'Greys_r', 'OrRd', 'OrRd_r', 'Oranges', 'Oranges_r', 
 'PRGn', 'PRGn_r', 'Paired', 'Paired_r', 'Pastel1', 'Pastel1_r', 'Pastel2', 
 'Pastel2_r', 'PiYG', 'PiYG_r', 'PuBu', 'PuBuGn', 'PuBuGn_r', 'PuBu_r', 'PuOr',
 'PuOr_r', 'PuRd', 'PuRd_r', 'Purples', 'Purples_r', 'RdBu', 'RdBu_r', 'RdGy', 
 'RdGy_r', 'RdPu', 'RdPu_r', 'RdYlBu', 'RdYlBu_r', 'RdYlGn', 'RdYlGn_r', 'Reds',
 'Reds_r', 'Set1', 'Set1_r', 'Set2', 'Set2_r', 'Set3', 'Set3_r', 'Spectral',
 'Spectral_r', 'Wistia', 'Wistia_r', 'YlGn', 'YlGnBu', 'YlGnBu_r', 'YlGn_r', 
 'YlOrBr', 'YlOrBr_r', 'YlOrRd', 'YlOrRd_r', 'afmhot', 'afmhot_r', 'autumn',
 'autumn_r', 'binary', 'binary_r', 'bone', 'bone_r', 'brg', 'brg_r', 'bwr',
 'bwr_r', 'cividis', 'cividis_r', 'cool', 'cool_r', 'coolwarm', 'coolwarm_r',
 'copper', 'copper_r', 'cubehelix', 'cubehelix_r', 'flag', 'flag_r','gist_earth',
 'gist_earth_r', 'gist_gray', 'gist_gray_r', 'gist_heat','gist_heat_r', 'gist_ncar',
 'gist_ncar_r', 'gist_rainbow', 'gist_rainbow_r','gist_stern', 'gist_stern_r',
 'gist_yarg', 'gist_yarg_r', 'gnuplot','gnuplot2', 'gnuplot2_r', 'gnuplot_r', 'gray',
 'gray_r', 'hot', 'hot_r', 'hsv', 'hsv_r', 'inferno', 'inferno_r', 'jet','jet_r',
 'magma', 'magma_r','nipy_spectral', 'nipy_spectral_r', 'ocean', 'ocean_r',
'pink', 'pink_r','plasma', 'plasma_r', 'prism', 'prism_r', 'rainbow', 'rainbow_r',
'seismic', 'seismic_r', 'spring', 'spring_r', 'summer', 'summer_r', 'tab10','tab10_r',
'tab20', 'tab20_r', 'tab20b', 'tab20b_r', 'tab20c', 'tab20c_r', 'terrain','terrain_r',
'turbo', 'turbo_r', 'twilight', 'twilight_r', 'twilight_shifted','twilight_shifted_r',
'viridis', 'viridis_r', 'winter', 'winter_r'
"""

matplotlib cmap取值问题

直接定义一个类来获取cmap中各个颜色方便使用 

使用的话:mycolor = MyColor(‘Accent’); mycolor.get_color();# 每次就调用获取下一个cmap中的颜色。

class MyColor(object):
    def __init__(self, cmap_name):
        self.color_set  = plt.get_cmap(cmap_name).colors
        self.idx = 0
        self.color_len = len(self.color_set)
        
    def get_color(self):
        if self.idx == self.color_len - 1:
            self.idx = 0
        color = self.color_set[self.idx]
        self.idx += 1
        return color

可视化官方提供的cmap

比如查看:[‘Pastel1’, ‘Pastel2’, ‘Paired’, ‘Accent’, ‘Dark2’, ‘Set1’, ‘Set2’, ‘Set3’, ‘tab10’, ‘tab20’, ‘tab20b’, ‘tab20c’]

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt

cmaps = {}
gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))

def plot_color_gradients(category, cmap_list):
    # Create figure and adjust figure height to number of colormaps
    nrows = len(cmap_list)
    figh = 0.35 + 0.15 + (nrows + (nrows - 1) * 0.1) * 0.22
    fig, axs = plt.subplots(nrows=nrows + 1, figsize=(6.4, figh), dpi=100)
    fig.subplots_adjust(top=1 - 0.35 / figh, bottom=0.15 / figh,
                        left=0.2, right=0.99)
    axs[0].set_title(f'{category} colormaps', fontsize=14)

    for ax, name in zip(axs, cmap_list):
        ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
        ax.text(-0.01, 0.5, name, va='center', ha='right', fontsize=10,
                transform=ax.transAxes)

    # Turn off *all* ticks & spines, not just the ones with colormaps.
    for ax in axs:
        ax.set_axis_off()

    # Save colormap list for later.
    cmaps[category] = cmap_list
    

plot_color_gradients('Qualitative',
                     ['Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2',
                      'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b',
                      'tab20c'])

运行后:

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Python数据结构树与算法分析

    Python数据结构树与算法分析

    这篇文章主要介绍了Python数据结构树与算法分析,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-07-07
  • Python报错error: subprocess-exited-with-error解决办法

    Python报错error: subprocess-exited-with-error解决办法

    在Python开发中,遇到subprocess-exited-with-error通常是由依赖缺失、权限问题、环境配置错误或兼容性问题导致,修复方法包括安装依赖、使用虚拟环境、提升权限、检查路径和命令,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-10-10
  • 分析Python字符串拼接+=和join()哪个速度更快

    分析Python字符串拼接+=和join()哪个速度更快

    这篇文章主要分析了Python中字符串拼接+=和join()哪个速度更快,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • 基于python使用OpenCV进行物体轮廓排序

    基于python使用OpenCV进行物体轮廓排序

    这篇文章主要介绍了基于python使用OpenCV进行物体轮廓排序,在进行图像处理过程中,我们经常会遇到一些和物体轮廓相关的操作,我们直接使用Opencv的findContours函数可以很容易的得到每个目标的轮廓,但是可视化后, 这个次序是无序的,更多相关资料请参考下面文章内容
    2022-01-01
  • python thread 并发且顺序运行示例

    python thread 并发且顺序运行示例

    以上源文件是对python中的线程的一个简单应用,实现了对并发线程的顺序运行,也许对你会有小小帮助
    2009-04-04
  • Python queue模块攻略全解

    Python queue模块攻略全解

    这篇文章主要为大家介绍了Python queue模块攻略全解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • Python3转换html到pdf的不同解决方案

    Python3转换html到pdf的不同解决方案

    今天小编就为大家分享一篇关于Python3转换html到pdf的不同解决方案,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • 利用Python的装饰器解决Bottle框架中用户验证问题

    利用Python的装饰器解决Bottle框架中用户验证问题

    这篇文章主要介绍了Python的Bottle框架中解决用户验证问题,代码基于Python2.x版本,需要的朋友可以参考下
    2015-04-04
  • keras 多gpu并行运行案例

    keras 多gpu并行运行案例

    这篇文章主要介绍了keras 多gpu并行运行案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06
  • 一文带你详解Python中sys.executable函数的作用

    一文带你详解Python中sys.executable函数的作用

    sys.executable函数是用来获取当前Python解释器的完整路径的,本文主要介绍了一文带你详解Python中sys.executable函数的作用,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03

最新评论