【问题标题】:How to view all colormaps available in matplotlib?如何查看 matplotlib 中可用的所有颜色图?
【发布时间】:2015-12-16 14:17:54
【问题描述】:

我想知道是否有一个函数调用可以为我提供 matplotlib 中所有可用颜色图的名称?

过去可以通过以下方式实现(参见here):

import matplotlib.pyplot as plt
cmaps = sorted(m for m in plt.cm.datad if not m.endswith("_r"))

但在 mpl 1.5 中运行它并不会返回新的颜色图,例如 viridismagma 等。在新的参考页面上,代码实际上对名称进行了硬编码(请参阅here),但我想知道是否仍然可以进行与上述类似的查询?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    plt.colormaps() 返回所有已注册颜色图的列表。来自文档:

    matplotlib.pyplot.colormaps()

    Matplotlib 提供了许多颜色图,其他的可以使用 register_cmap() 添加。该函数记录了内置的颜色图,如果被调用,还将返回所有已注册颜色图的列表。

    此返回的列表包括viridismagmainfernoplasma 对我来说1.5.0

    【讨论】:

      【解决方案2】:

      这里有一些代码可以绘制链接到其 ID 的所有可用颜色图

      import matplotlib as mpl
      import matplotlib.pyplot as plt
      
      def plot_colorMaps(cmap):
      
          fig, ax = plt.subplots(figsize=(4,0.4))
          col_map = plt.get_cmap(cmap)
          mpl.colorbar.ColorbarBase(ax, cmap=col_map, orientation = 'horizontal')
      
          plt.show()
      
      for cmap_id in plt.colormaps():
          print(cmap_id)
          plot_colorMaps(cmap_id)
      

      输出如下所示

      口音

      Accent_r

      蓝调

      等等……

      【讨论】:

      • 这个答案被低估了。这会显示 every 颜色图,尤其是 matplotlib 文档的示例颜色图页面上未列出的颜色图。非常感谢!
      【解决方案3】:

      下面是基于 pr94 方法的代码,它在标题中使用其 id 绘制每个颜色图,但不会为每个颜色图打开一个图。
      我用它在一张图片中创建了所有 166 个颜色图的概览(mpl 版本 3.4.2)。

      import matplotlib as mpl
      import matplotlib.pyplot as plt
      
      def plot_all_cmaps():
          N_ROWS, N_COLS = 8, 7 # 13, 13 <-- for all in one figure 
          HEIGHT, WIDTH = 7, 14
      
          cmap_ids = plt.colormaps()
          n_cmaps = len(cmap_ids)
          
          print(f'mpl version: {mpl.__version__},\nnumber of cmaps: {n_cmaps}')
          
          index = 0
          while index < n_cmaps:
              fig, axes = plt.subplots(N_ROWS, N_COLS, figsize=(WIDTH, HEIGHT))
              for row in range(N_ROWS):
                  for col in range(N_COLS):
                      ax = axes[row, col]
                      cmap_id = cmap_ids[index]
                      cmap = plt.get_cmap(cmap_id)
                      mpl.colorbar.ColorbarBase(ax, cmap=cmap,
                                                orientation='horizontal')
                      ax.set_title(f"'{cmap_id}', {index}", fontsize=8)
                      ax.tick_params(left=False, right=False, labelleft=False,
                                     labelbottom=False, bottom=False)
                      
                      last_iteration = index == n_cmaps-1
                      if (row==N_ROWS-1 and col==N_COLS-1) or last_iteration:
                          plt.tight_layout()
                          #plt.savefig('colormaps'+str(index)+'.png')
                          plt.show()
                          if last_iteration: return
                      index += 1
      
      plot_all_cmaps()
      

      【讨论】:

        【解决方案4】:

        您似乎可以通过调用它的名称来直接查看颜色图实例。例如,col_map = plt.get_cmap(cmap),然后在终端中输入 col_map(例如 spyder),颜色图将显示为绘图。但是,我不确定是什么触发了这种外观,或者是否有任何必要的代码来指定此选项?

        【讨论】:

          【解决方案5】:

          截至 2021 年(matplotlib 3.4),

          颜色图对象的 IPython 表示

          matplotlib.colors.Colormap 对象现在具有 IPython / Jupyter 后端的图像表示。在最后一行返回颜色图的单元格将显示颜色图的图像。

          所以现在您可以使用cmaps = plt.colormaps() 来获取所有有效的颜色图名称。输出将是一个名称列表:

          ['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']
          

          然后plt.get_cmap(cmaps[0]) 将在 IPython/JupyetrLab 中渲染颜色图。

          【讨论】:

            【解决方案6】:

            如上所述,plt.colormaps() 将返回 MPL 中所有已注册颜色图的列表。 如果您还想绘制所有这些,以便您可以看到它们的样子,我建议您查看 CMasher 包中的 create_cmap_overview() 函数 (https://cmasher.readthedocs.io/user/usage.html#colormap-overviews)。 它需要一个颜色图列表,并使用它提供的所有颜色图创建一个概览图。 如果您想在特定方面对颜色图进行排序或按类型对它们进行分组,它还有许多选项可供您使用。

            可以在这里看到一个例子(在这篇文章中没有显示图片,因为它相当大):https://cmasher.readthedocs.io/user/cmap_overviews/mpl_cmaps.html

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-03-12
              • 2017-09-17
              • 1970-01-01
              • 2021-09-11
              相关资源
              最近更新 更多