heatmap可视化相似矩阵

博客介绍了使用Python绘制可视化相似矩阵的热力图,包括呈现正方形格仔、去除纵横轴label、去除color bar等效果,还提及解决plt.savefig()保存图片白边问题,主要运用seaborn.heatmap和matplotlib。

可视化相似矩阵 S ∈ R n × n S\in\mathbb{R}^{n\times n} SRn×n 用来画图,效果:

  • 正方形格仔
  • 去除纵、横轴 label
  • 去除 color bar
  • 去除白边

heatmap

Code

import numpy as np
import seaborn as sns
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt


S = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
              [2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
              [1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
              [0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
              [0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
              [1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
              [0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])

ax = sns.heatmap(S, cmap="YlGn",
                 square=True,  # 正方形格仔
                 cbar=False,  # 去除 color bar
                 xticklabels=False, yticklabels=False)  # 去除纵、横轴 label
fig = ax.get_figure()
fig.savefig('class_sim_sub.jpg', bbox_inches='tight',
            pad_inches=0.0)  # 去白边

References

  1. Creating annotated heatmaps
  2. seaborn.heatmap
  3. 【画图】Python绘制热力图方法以及保存不全问题(seaborn.heatmap)
  4. 去除plt.savefig()的白边 Removing white space around a saved image in matplotlib
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值