colormap颜色映射

[python] view plain copy
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() 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
举例:
横坐标是从1到1000的数,纵坐标是横坐标的平方,要求使用颜色映射为:gist_rainbow(即最后一张图的倒数第四个颜色映射)

import matplotlib.pyplot as plt

x = list(range(1,1001))
y = [i**2 for i in x]#使用列表解析

plt.scatter(x,y,c=y,cmap=plt.cm.gist_rainbow,s=20)#cm即colormap,c=y表示颜色随y变化
plt.xlabel('number',fontsize=10)
plt.ylabel('number square',fontsize=10)
plt.title('figure 1',fontsize =20)
plt.axis([0,1100,0,1100000])
plt.show()

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

代码中cm即colormap,c=y表示颜色随y变化(第一图),也可以使用c=x,使得颜色随x映射(第二图),变化不大
图如下:
这里写图片描述
这里写图片描述

</article>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值