用matplotlib画热力图(日记)

借鉴原文如下:https://blog.csdn.net/coder_Gray/article/details/81867639

真的是找了两天才找到既简洁又能满足我全部需求的例子,感谢作者了。

根据需求做了如下修改

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

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
 
def draw(xLabel,yLabel,heatlist,title):    
    #作图阶段
    fig = plt.figure(dpi =200)
    #定义画布为1*1个划分,并在第1个位置上进行作图
    ax = fig.add_subplot(111)
    #定义横纵坐标的刻度
    ax.set_xticks(np.arange(len(xLabel)), labels=xLabel)
    ax.set_yticks(np.arange(len(yLabel)), labels=yLabel)
    #作图并选择热图的颜色填充风格,这里选择hot
    im = ax.imshow(heatlist, cmap=plt.cm.hot_r)
  
    # Rotate the tick labels and set their alignment.
    plt.setp(ax.get_xticklabels(), rotation=45, ha="right",rotation_mode="anchor")
    # Loop over data dimensions and create text annotations.
    for i in range(len(yLabel)):
        for j in range(len(xLabel)):
            text = ax.text(j, i, heatlist[i, j],ha="center", va="center", color="w")
    #增加右侧的颜色刻度条
    plt.colorbar(im,fraction=0.025, pad=0.05)
    #增加标题
    plt.title(title)
    #show
    plt.show()
    
heatlist1 = np.array([[1, 0, 2, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 2, 0],
                    [0, 0, 1, 0, 0, 1, 0, 2, 0, 0, 1, 0, 0, 1, 2, 4, 4, 3, 7, 10, 2, 0, 1, 3],
                    [25, 8, 0, 6, 7, 12, 6, 7, 19, 1,  4, 0, 3,  3,  6,  8, 21, 9, 0, 2, 5, 5, 4, 23],
                    [3, 4,6, 16, 38, 21, 8, 2, 3, 9, 17, 7, 12, 13, 19, 35, 42, 12, 1,  8, 17,  5, 28, 21],
                    [27, 24, 5, 24, 28, 23, 23, 45, 93, 59, 56, 31, 12, 20, 16, 23, 21,  20,  6,  4, 15, 16, 11,  8],
                    [11,  7, 21, 18, 35, 79, 59, 30, 14, 22, 38, 13, 23, 29, 42, 61, 80,  74, 31, 19, 42, 60, 69, 49],
                    [22, 15,  6, 15,  3,  4, 13,  5, 13, 12, 26, 22, 53, 48, 22, 43, 42,  65, 80, 38, 49, 51, 40, 16],
                    [5, 5, 7, 1,0,5, 25, 0,1, 3, 2,0,  3, 10,  8,  1,  4,  8,  3,  9,  1,  2,  6, 13],
                    [0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,0],
                    [0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
                    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0]])
#定义热图的横纵坐标
xLabel1 = ["0", "1", "2","3", "4", "5", "6","7", "8", "9","10", "11", "12", "13",
          "14", "15","16", "17", "18", "19","20", "21", "22","23"]
yLabel1 = ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]

title1='@#%$^%$&年(&&^HTG市短时强降水月、时次数变化分布'
d = draw(xLabel1,yLabel1,heatlist1,title1)
 

 

ax.imshow(heatlist, cmap=plt.cm.hot_r)

这是作图的最关键一行。

官方原文在https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.imshow.html?highlight=imshow#matplotlib.axes.Axes.imshow

Axes.imshow(X, cmap=None, norm=None, *, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, origin=None, extent=None, interpolation_stage=None, filternorm=True, filterrad=4.0, resample=None, url=None, data=None, **kwargs)

可选参数很多,最关键的就是二维数组X,根据自己颜色需求选用合适的cmap值。

1 hot 从黑平滑过度到红、橙色和黄色的背景色,然后到白色。
2 cool 包含青绿色和品红色的阴影色。从青绿色平滑变化到品红色。
3 gray 返回线性灰度色图。
4 bone 具有较高的蓝色成分的灰度色图。该色图用于对灰度图添加电子的视图。
5 white 全白的单色色图。 
6 spring 包含品红和黄的阴影颜色。 
7 summer 包含绿和黄的阴影颜色。
8 autumn 从红色平滑变化到橙色,然后到黄色。 
9 winter 包含蓝和绿的阴影色。

 

posted @ 2022-08-01 10:57  EROEG  阅读(542)  评论(0编辑  收藏  举报