matplotlib的cmap

plt.scatter(X[:, 0], X[:, 1], c=data_index, alpha=0.6, cmap=plt.cm.get_cmap('Set1', class_num))

在这里用到了cmap这个参数
matplotlib中用来做热图的颜色映射有很多种,我们可以选择各种不同的颜色映射来做出符合我们预期的热图。其源代码如下所示:

本代码是根据官网(http://matplotlib.org/examples/color/colormaps_reference.html )的源代码转载过来的

import numpy as np  
import matplotlib.pyplot as plt  
  
# Have colormaps separated into categories:  
# http://matplotlib.org/examples/color/colormaps_reference.html  
  
cmaps = [('Perceptually Uniform Sequential',  
                            ['viridis', 'inferno', 'plasma', 'magma']),  
         ('Sequential',     ['Blues', 'BuGn', 'BuPu',  
                             'GnBu', 'Greens', 'Greys', 'Oranges', 'OrRd',  
                             'PuBu', 'PuBuGn', 'PuRd', 'Purples', 'RdPu',  
                             'Reds', 'YlGn', 'YlGnBu', 'YlOrBr', 'YlOrRd']),  
         ('Sequential (2)', ['afmhot', 'autumn', 'bone', 'cool',  
                             'copper', 'gist_heat', 'gray', 'hot',  
                             'pink', 'spring', 'summer', 'winter']),  
         ('Diverging',      ['BrBG', 'bwr', 'coolwarm', 'PiYG', 'PRGn', 'PuOr',  
                             'RdBu', 'RdGy', 'RdYlBu', 'RdYlGn', 'Spectral',  
                             'seismic']),  
         ('Qualitative',    ['Accent', 'Dark2', 'Paired', 'Pastel1',  
                             'Pastel2', 'Set1', 'Set2', 'Set3']),  
         ('Miscellaneous',  ['gist_earth', 'terrain', 'ocean', 'gist_stern',  
                             'brg', 'CMRmap', 'cubehelix',  
                             'gnuplot', 'gnuplot2', 'gist_ncar',  
                             'nipy_spectral', 'jet', 'rainbow',  
                             'gist_rainbow', 'hsv', 'flag', 'prism'])]  
  
  
nrows = max(len(cmap_list) for cmap_category, cmap_list in cmaps)  
gradient = np.linspace(0, 1, 256)  
gradient = np.vstack((gradient, gradient))  
  
  
def plot_color_gradients(cmap_category, cmap_list):  
    fig, axes = plt.subplots(nrows=nrows)  
    fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.99)  
    axes[0].set_title(cmap_category + ' colormaps', fontsize=14)  
  
    for ax, name in zip(axes, cmap_list):  
        ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))  
        pos = list(ax.get_position().bounds)  
        x_text = pos[0] - 0.01  
        y_text = pos[1] + pos[3]/2.  
        fig.text(x_text, y_text, name, va='center', ha='right', fontsize=10)  
  
    # Turn off *all* ticks & spines, not just the ones with colormaps.  
    for ax in axes:  
        ax.set_axis_off()  
  
for cmap_category, cmap_list in cmaps:  
    plot_color_gradients(cmap_category, cmap_list)  
  
plt.show() 

Perceptually Uniform Sequential、Sequential、Sequential (2)、Diverging、Qualitative、Miscellaneous对应的列表中的值均可作为cmap的值。

其效果图如下所示:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 13
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值