赞
踩
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
实例:
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数据结构:
经过重塑数据格式变为(df.pivot):
heatmap:
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)
在使用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()
还需要继续优化,后面继续修改~
更新:
(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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。