翼度科技»论坛 编程开发 python 查看内容

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

5

主题

5

帖子

15

积分

新手上路

Rank: 1

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

代码如下:

方法一
  1. import matplotlib.pyplot as plt

  2. cmaps = sorted(m for m in plt.cm.datad if not m.endswith("_r"))
  3. print(cmaps)
复制代码
我们忽略以_r结尾的类型,因为它们都是类型后面不带有_r的反转版本(reversed version)。
所有的类型我们可以在matplotlib的源代码中找到:(如下图)


方法二
  1. import matplotlib.pyplot as plt

  2. cmap_list1 = plt.colormaps()
  3. print(cmap_list1)
复制代码
方法三

如果使用的是Pycharm编译器,那么可以在作图的时候简单的随便给定一个cmap的类型,如果给定的cmap类型是错误的,那么在编译器的错误提示信息中也会显示出所有的cmap类型。
比如,我们这里我们想要做一个高斯函数的曲面分布图,我们随意给cmap一个'aaa'的值,这时,我们可以在编译器提示窗口看到如下错误信息的输出。
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from mpl_toolkits.mplot3d import Axes3D

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

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


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

使用的话:mycolor = MyColor(‘Accent’); mycolor.get_color();# 每次就调用获取下一个cmap中的颜色。
  1. class MyColor(object):
  2.     def __init__(self, cmap_name):
  3.         self.color_set  = plt.get_cmap(cmap_name).colors
  4.         self.idx = 0
  5.         self.color_len = len(self.color_set)
  6.         
  7.     def get_color(self):
  8.         if self.idx == self.color_len - 1:
  9.             self.idx = 0
  10.         color = self.color_set[self.idx]
  11.         self.idx += 1
  12.         return color
复制代码
可视化官方提供的cmap

比如查看:[‘Pastel1’, ‘Pastel2’, ‘Paired’, ‘Accent’, ‘Dark2’, ‘Set1’, ‘Set2’, ‘Set3’, ‘tab10’, ‘tab20’, ‘tab20b’, ‘tab20c’]
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import matplotlib.pyplot as plt

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

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

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

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

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

  25. plot_color_gradients('Qualitative',
  26.                      ['Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2',
  27.                       'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b',
  28.                       'tab20c'])
复制代码
运行后:

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

来源:https://www.jb51.net/article/266619.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具