美文网首页
Python绘制热图

Python绘制热图

作者: 陈光辉_山东花生 | 来源:发表于2020-03-06 22:24 被阅读0次

1.matplitlib绘制热图

import random
from matplotlib import pyplot as plt
from matplotlib import cm
from matplotlib import axes

def draw():
    # define the x and y axis of the hotmap
    xLabel = ['A', 'B', 'C', 'D', 'E']
    yLabel = ['1', '2', '3', '4', '5']

    # prepaer the data, generate the two-dimension data
    data = []
    for i in range(5):
        temp = []
        for j in range(5):
            k = random.randint(0,100)
            temp.append(k)
        data.append(temp)

    # plot the figure
    fig = plt.figure()
    ax = fig.add_subplot(111)
    # define the scale
    ax.set_yticks(range(len(yLabel)))
    ax.set_yticklabels(yLabel, fontproperties=plt.cm.hot_r)
    ax.set_xticks(range(len(xLabel)))
    ax.set_xtickslabels(xLabel)
    # make the figure and select the style of hotmap
    im = ax.imshow(data, cmap = plt.cm.hot_r) # the value is more high, the color is more deep
    # im = ax.imshow(data, cmap = plt.cm.hot) # the value is more high, the color is more shallow
    # plt.cm.~ hot, cool, gray, bone, white, spring, summer, autumn, winter

    # add the scale bar of the right site
    plt.colorbar(im)

    plt.title("This is a title")
    plt.show()

d = draw()

2.Seaborn绘制热图

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
sns.set()
# data 1
np.random.seed(0)
uniform_data = np.random.rand(10, 12, vmin = 0, vmax = 1, center = 0)
# vmin=0, vmax=1 : the scope of colorbar value 
# center=0 :  colorbar valuee centered at 0
ax = sns.heatmap(uniform_data)

# data 2
flights_long = sns.load_dataset("flights")
flights = flights_long.pivot("month", "year", "passengers")
f, ax = plt.subplots(figsize = (9, 6))
# annot = True: show the value
sns.heatmap(flights, annot = True, fmt = 'd', cmap = 'Y|GnBu', linewidth = 5, ax = ax)
label_y = ax.get_yticklabels()
plt.setp(label_y, rotation = 360, horizontalalignment = 'right')
label_x = ax.get_xticklabels()
plt.setp(label_x, rotation = 45, horizontalalignment = 'right')

plt.show()

# https://www.jianshu.com/p/d105d934d876

相关文章

网友评论

      本文标题:Python绘制热图

      本文链接:https://www.haomeiwen.com/subject/rjkarhtx.html