python 绘制 heatmap

1. colormap

Python提供了各种各样的colormap, 有三类: 亮度连续变化(Sequence), 亮度不连续(Diverging), 定性的,颜色之间没有次序(Qualitative)。

2. 将数据矩阵的数值映射到颜色,可以采用matplotlib.pyplot中的imshow函数,当然你首先需要colormap,像如下这样:

  

import matplotlib.pyplot as plt

def draw_heatmap(data, map_name, vmin, vmax):
    cmap = cm.get_cmap('rainbow', 1000)
    h, w = len(data), len(data[0])
    figure = plt.figure(figsize=(int(w*1.2/32), int(h*1.2/32)))
    plt.figure(facecolor='w')
    ax = figure.add_subplot(111)
    #ax.set_yticks([])
    #ax.set_xticks([])

    ax.axis("off")
    map = ax.imshow(data, cmap=cmap, extent=None, aspect='equal', vmin=vmin, vmax=vmax, )
    print 'type', type(map)
    #fig = plt.gcf()
    # cb = plt.colorbar(mappable=map, cax=None, ax=None, shrink=0.5)
    # plt.savefig(map_name, dpi=32,  bbox_inches='tight', pad_inches=0.0)
    plt.savefig(map_name, bbox_inches='tight', dpi=32)

    plt.show()

猜你喜欢

转载自blog.csdn.net/easywaytolifebelief/article/details/77969968