【基础】学习笔记53-Python3 matplotlib绘图-热力图2

热力图2

运行结果为:


代码如下:

# 随机热力图:imshow

import matplotlib.pyplot as plt

import numpy as np

import seaborn as sns

plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']

'''

# ===========随机热力图==========

x = np.random.rand(10)

y = np.random.rand(10)

z = (np.random.rand(9000000) + np.linspace(0, 1, 9000000)

    ).reshape(3000, 3000)  # reshape函数将原矩阵A,重组为新矩阵B

im = plt.imshow(z, interpolation='nearest', cmap='hot',

                extent=(np.amin(x), np.amax(x), np.amin(

                    y), np.amax(y))  # 设置坐标轴范围

                )

plt.colorbar(im, shrink=0.5)  # 设置Bar

plt.show() '''

def Imshow_heatmap(data, xlabel, ylabel):  # imshow绘制矩阵随机热力图

    # 使用figure

    # plt.figure(facecolor='w')

    #plt.subplot(111, position=[0.1, 0.15, 0.8, 0.8])

    plt.yticks(range(len(ylabel)), ylabel)

    plt.xticks(range(len(xlabel)), xlabel)

    '''

    # 使用Figure显式的创建Axes

    fig = plt.figure(facecolor='w')

    ax = fig.add_subplot(111, position=[0.1, 0.15, 0.8, 0.8])

    ax.set_yticks(range(len(ylabel)))

    ax.set_yticklabels(ylabel)  # 用ylabel替代y坐标值

    ax.set_xticks(range(len(xlabel)))

    ax.set_xticklabels(xlabel)  # 用xlabel替代x坐标值'''

    # 求data数组的最小值和最大值

    vmax, vmin = data[0][0], data[0][0]

    for i in data:

        if min(i) < vmin:

            vmin = min(i)

        if max(i) > vmax:

            vmax = max(i)

    map = plt.imshow(data, interpolation='nearest',

                    cmap='summer', vmin=vmin, vmax=vmax)

    plt.colorbar(map, shrink=0.5)

    plt.show()

if __name__ == "__main__":

    data = np.random.rand(10, 10)

    xlabel = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']

    ylabel = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

    Imshow_heatmap(data, xlabel, ylabel)

你可能感兴趣的:(【基础】学习笔记53-Python3 matplotlib绘图-热力图2)