美文网首页
Python-科学计算-seaborn-02-热力图

Python-科学计算-seaborn-02-热力图

作者: Data_Python_VBA | 来源:发表于2019-12-04 19:47 被阅读0次

微信公众号原文

系统:Windows 7
语言版本:Anaconda3-4.3.0.1-Windows-x86_64
编辑器:pycharm-community-2016.3.2

  • 这个系列讲讲Python的科学计算版块
  • 今天讲讲seaborn模块:热力图

Part 1:示例

  1. 已知df_1,有4列["p1", "p2", "p3", "p4"]
  2. 根据这4列做热力图,结果如下图

热力图

1.png

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()



代码截图

3.png

df_1

2.png

Part 3:部分代码解读

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

cmap="hot_r"效果图

5-hot_r.png

cmap="YlGnBu"效果图

4-YlGnBu.png

本文为原创作品,欢迎分享朋友圈

长按图片识别二维码,关注本公众号
Python 优雅 帅气


12x0.8.jpg

相关文章

网友评论

      本文标题:Python-科学计算-seaborn-02-热力图

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