系统:Windows 7
语言版本:Anaconda3-4.3.0.1-Windows-x86_64
编辑器:pycharm-community-2016.3.2
- 这个系列讲讲Python的科学计算版块
- 今天讲讲seaborn模块:热力图
Part 1:示例
- 已知
df_1
,有4列["p1", "p2", "p3", "p4"]
- 根据这4列做热力图,结果如下图
热力图

Part 2:代码
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
dict_1 = {
"p1": [0.5, 0.8, 1.0, 1.2, 1.5, 2.5, 0.9, 0.6, 1.3, 1.0,
1.3, 1.6, 1.9, 2.5, 4.2, 3.5, 2.2, 1.2, 1.5, 0.5],
"p2": [1.3, 2.8, 1.3, 1.4, 6.5, 2.5, 0.9, 0.6, 1.3, 1.0,
1.3, 1.6, 1.9, 2.5, 4.2, 3.5, 1.2, 1.2, 3.5, 2.5],
"p3": [2.5, 0.8, 1.3, 1.2, 1.5, 2.8, 1.9, 0.6, 1.3, 1.1,
1.3, 1.6, 1.1, 2.5, 4.2, 3.9, 2.2, 1.2, 1.5, 0.5],
"p4": [2.5, 0.8, 1.3, 1.2, 1.5, 3.8, 1.9, 0.6, 1.3, 1.1,
1.3, 1.6, 1.1, 2.5, 4.2, 3.9, 2.2, 1.2, 1.5, 0.5]}
df_1 = pd.DataFrame(dict_1, columns=["p1", "p2", "p3", "p4"])
print(df_1)
sns.set(style="ticks", color_codes=True)
sns.heatmap(df_1, linewidths=0.05, linecolor='yellow', vmin=0, vmax=5, annot=True)
plt.show()
代码截图

df_1

Part 3:部分代码解读
-
sns.heatmap(df_1, linewidths=0.05, linecolor='yellow', vmin=0, vmax=5, annot=True)
-
linewidths
设置每个单元格的线宽 -
linecolor
设置单元格的线框的颜色 -
vmin=0, vmax=5
设置颜色区间的最小最大值 -
annot=True
在每个单元格中显示具体的数值
-
- 如果对于生成的热力图颜色不是很喜欢,可以更改,通过增加一个新参数
cmap
,该参数取值不同时,颜色地图也不同sns.heatmap(df_1, linewidths=0.05, linecolor='yellow', vmin=0, vmax=5, annot=True, cmap="hot_r")
cmap="hot_r"效果图

cmap="YlGnBu"效果图

本文为原创作品,欢迎分享朋友圈
长按图片识别二维码,关注本公众号
Python 优雅 帅气

网友评论