python绘制热力图

1.seaborn 绘制热力图
官方说明:https://seaborn.pydata.org/generated/seaborn.heatmap.html

语法:

seaborn.heatmap(data, *, vmin=None, vmax=None, cmap=None, center=None, robust=False, annot=None, fmt=‘.2g’, annot_kws=None, linewidths=0, linecolor=‘white’, cbar=True, cbar_kws=None, cbar_ax=None, square=False, xticklabels=‘auto’, yticklabels=‘auto’, mask=None, ax=None, **kwargs)

具体参数见:https://www.cnblogs.com/XiaoJ-cs/p/15159834.html

  • data:矩阵数据集,可以使numpy的数组(array),如果是pandas的dataframe,则df的index/column信息会分别对应到heatmap的columns和rows;dataframe可以用df.pivot(index=‘1’, columns=‘2’, values=‘3’);
  • linewidths,热力图矩阵之间的间隔大小;
  • vmax,vmin, 图例中最大值和最小值的显示值,没有该参数时默认不显示。
  • fmt,格式设置,决定annot注释的数字格式,小数点后几位等;
  • cbar : 是否画一个颜色条,是否在热力图侧边绘制颜色刻度条,默认值是True;
  • cbar_kws : 颜色条的参数,相关字体设置,默认值是None;
  • cbar_ax:热力图侧边绘制颜色刻度条时,刻度条位置设置,默认值是None;
  • square:设置热力图矩阵小块形状。

实例:
https://www.statology.org/seaborn-heatmap/

#import seaborn
import seaborn as sns

#load "flights" dataset
data = sns.load_dataset("flights")
data = data.pivot("month", "year", "passengers")
sns.heatmap(data)

flights数据结构:
python绘制热力图_第1张图片
经过重塑数据格式变为(df.pivot):
python绘制热力图_第2张图片
heatmap:
python绘制热力图_第3张图片

2.用dataframe格式数据绘制热力图

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# load the sample data
df = pd.DataFrame({'MutProb': [0.1,
  0.05, 0.01, 0.005, 0.001, 0.1, 0.05, 0.01, 0.005, 0.001, 0.1, 0.05, 0.01, 0.005, 0.001, 0.1, 0.05, 0.01, 0.005, 0.001, 0.1, 0.05, 0.01, 0.005, 0.001], 'SymmetricDivision': [1.0, 1.0, 1.0, 1.0, 1.0, 0.8, 0.8, 0.8, 0.8, 0.8, 0.6, 0.6, 0.6, 0.6, 0.6, 0.4, 0.4, 0.4, 0.4, 0.4, 0.2, 0.2, 0.2, 0.2, 0.2], 'test': ['sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule', 'sackin_yule'], 'value': [-4.1808639999999997, -9.1753490000000006, -11.408113999999999, -10.50245, -8.0274750000000008, -0.72260200000000008, -6.9963940000000004, -10.536339999999999, -9.5440649999999998, -7.1964070000000007, -0.39225599999999999, -6.6216390000000001, -9.5518009999999993, -9.2924690000000005, -6.7605589999999998, -0.65214700000000003, -6.8852289999999989, -9.4557760000000002, -8.9364629999999998, -6.4736289999999999, -0.96481800000000006, -6.051482, -9.7846860000000007, -8.5710630000000005, -6.1461209999999999]})

# pivot the dataframe from long to wide form
result = df.pivot(index='SymmetricDivision', columns='MutProb', values='value')

sns.heatmap(result, annot=True, fmt="g", cmap='viridis')
plt.show()

df.pivot函数:用来重塑数据的函数,构造透视表

pivot(index=None, columns=None, values=None)

  • index: 可选参数。设置新dataframe的行索引,如果未指明,就用当前已存在的行索引。
  • columns:必选参数。用来设置作为新dataframe的列索引。
  • values:可选参数。在原dataframe中选中某一列/几列的值,使其在新dataframe的列里显示。如果不指定,则默认将原dataframe中所有的列都显示,这里需要注意:为了将所有的值都显示出来,就会出现多层行索引的情况。

在使用df.pivot函数时报错:
在这里插入图片描述
报错原因:
index和columns同时有相同的值。
解决方法:
删除相同行或合并相同行。
或参考这篇文章的方法:df1.reset_index().pivot_table(values=3, index=[0, 1], columns=2, aggfunc='mean')

3.优化热力图
可参考这篇文章:https://zhuanlan.zhihu.com/p/444175795
(1)设置y轴刻度

df[u'a'] = df[u'a'].apply(lambda x: format(x, '.2f'))#保留两位小数

(2)seaborn.heatmap刻度及标签设置
https://www.cnblogs.com/IvanSSSS/p/12586578.html

(3)设置color bar

import pandas as pd
data = pd.DataFrame({'x':(1,2,3,4),'y':(1,2,3,4),'z':(14,15,23,2)})

sns.heatmap(data.pivot_table(index='y', columns="x", values="z"))
ax = sns.heatmap(data.pivot_table(index='y', columns="x", values="z"))
ax.collections[0].colorbar.set_label("Hello")

或直接设置其属性信息:sns.heatmap(data.pivot_table(index='y', columns="x", values="z"), cbar_kws={'label': 'colorbar title'})

4.整体测试代码和结果

import os
import numpy as np
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb
from pylab import mpl
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

#test
df = pd.read_excel(r'./heatmap_data1.xlsx')
df[u'Latitude'] = df[u'Latitude'].apply(lambda x: format(x, '.1f'))
df = df.reset_index().pivot_table("values", "Latitude", "Year")

mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = 'NSimSun,Times New Roman'
font = {'family': 'sans-serif',
            'color': 'k',
            'weight': 'normal',
            'size': 20,}

fig, ax = plt.subplots(figsize=(12, 10))
cmap = sb.diverging_palette(0, 230, 90, 60, as_cmap=True)

sns.heatmap(df,
            annot = False,
            fmt= ".2f",
            cmap = cmap,            
            vmin=-5, vmax=5,
            cbar_kws={"shrink": .8,"orientation": "horizontal"}, 
            linewidths = 0,
            xticklabels=2, yticklabels=8) 

plt.xlabel('Year',fontsize=20, color='k') 
plt.ylabel('Latitude',fontsize=20, color='k') 
plt.xticks(fontsize=20) 
plt.yticks(fontsize=20,rotation=-360)

cax = plt.gcf().axes[-1]
cax.tick_params(labelsize=20) 
cbar = ax.collections[0].colorbar
cbar.set_label(r'values',fontdict=font)

plt.show()

python绘制热力图_第4张图片
还需要继续优化,后面继续修改~

更新:
(1)修改colorbar:cbar_kws={"shrink": 1,'extend':'both'}

参考资料:
https://zhuanlan.zhihu.com/p/444175795

seaborn自带数据集:https://github.com/mwaskom/seaborn-data

绘制热力图seaborn.heatmap,cmap设置颜色的参数:https://blog.csdn.net/ztf312/article/details/102474190

热力图很好的例子:https://blog.csdn.net/bai_bai123/article/details/114456610

你可能感兴趣的:(绘图,#,python,python)