python 画热力图

# encoding:utf-8
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib import cm
from matplotlib import axes
import pylab

pylab.mpl.rcParams['font.sans-serif'] = ['SimHei']  # 防止中文乱码
pylab.mpl.rcParams['axes.unicode_minus'] = False  # 防止中文乱码


def draw_heatmap(data, xlabels, ylabels):
    cmap = cm.Blues
    figure = plt.figure(facecolor='w')
    ax = figure.add_subplot(2, 1, 1, position=[0.1, 0.15, 0.8, 0.8])
    ax.set_yticks(range(len(ylabels)))
    ax.set_yticklabels(ylabels)
    ax.set_xticks(range(len(xlabels)))
    ax.set_xticklabels(xlabels)
    vmax = data[0][0]
    vmin = data[0][0]
    for i in data:
        for j in i:
            if j > vmax:
                vmax = j
            if j < vmin:
                vmin = j
    map = ax.imshow(data, interpolation='nearest', cmap=cmap, aspect='auto', vmin=vmin, vmax=vmax)
    cb = plt.colorbar(mappable=map, cax=None, ax=None, shrink=0.5)
    plt.xticks(rotation=90)  # 将字体进行旋转
    plt.yticks(rotation=360)
    plt.show()


dataFrame = pd.read_excel('E:\\GEO_DOC\\2020年6月份\\月\\allRes2.xlsx', sheet_name="allRes2", encoding='gbk')
# 包含每个柱子对应值的序列
val1 = dataFrame.head(11)[['mixedProportion','fromWin','toWin']]

xlabels = ['x1','x2','x3']
ylabels = ['y1', 'y2', 'y3']
draw_heatmap(val1.values, xlabels, ylabels)

# encoding:utf-8
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib import cm
from matplotlib import axes
import pylab

pylab.mpl.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
pylab.mpl.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号


def draw_heatmap(data, xlabels, ylabels):
    """
    画热力图函数
    :param data: 数据,是一个二维数组
    :param xlabels: 横轴坐标名称
    :param ylabels: 纵轴坐标名称
    :return: null
    """

    figure = plt.figure()  # 画布特征,默认即可,可根据喜好调整
    ax = figure.add_subplot()  # 添加一个子画布
    ax.set_xticks(range(len(xlabels)))  # 横坐标长度
    ax.set_xticklabels(xlabels)  # 横坐标标签
    ax.set_yticks(range(len(ylabels)))  # 纵坐标长度
    ax.set_yticklabels(ylabels)  # 纵坐标标签

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

    #  获取二维数组中的 最大和最小两个值
    for i in data:  # 遍历二维数组,每次i获取到的是一维数组
        for j in i:  # 遍历一维数组,每次获取到的是一个数字
            if j > vmax:
                vmax = j
            if j < vmin:
                vmin = j

    cmap = cm.Reds  # 图像的颜色
    # interpolation 用来控制显示,参考https://matplotlib.org/gallery/images_contours_and_fields/interpolation_methods.html?highlight=interpolation
    map = ax.imshow(data, interpolation='nearest', cmap=cmap, aspect='auto', vmin=vmin, vmax=vmax)
    plt.colorbar(mappable=map, cax=None, ax=None, shrink=0.5)  # 添加右侧的色带
    plt.xticks(rotation=90)  # 横轴将字体进行旋转 90 度
    plt.yticks(rotation=360)  # 纵轴将字体进行旋转 0 度
    plt.show()  # 显示图像

def get_labels(num,label):
    '''
    获取一个数组
    :param num: 数量
    :param label: 标签前缀
    :return: 数组
    '''
    arr = []
    for i in range(0, num):
        arr.append(label + str(i))
    return arr

# main 函数,程序的入口
if __name__ == '__main__':
    # 从Excel 中读取数据
    dataFrame = pd.read_excel('E:\\GEO_DOC\\2020年6月份\\月\\allRes2.xlsx', sheet_name="allRes2", encoding='gbk')
    # 从 dataFrame 中提取三行数据,列为mixedProportion,fromWin,toWin
    # 根据需求提取数据
    val1 = dataFrame.head(10)[['mixedProportion', 'fromWin', 'toWin']]

    # 坐标轴坐标名称,要和数据对的上哈
    xlabels = get_labels(val1.shape[1], "x")  # shape 输出数组的行和列数
    ylabels = get_labels(val1.shape[0], "y")
    # 执行画热泪图的函数
    draw_heatmap(val1.values, xlabels, ylabels)

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值