本笔记来源于B站Up主: 有Li 的影像组学的系列教学视频
本节(26)主要讲解: 影像组学特征相关系数热图heatmap的Python实现
1、导入工具包
# The code below is modified from
# https://www.machinelearningplus.com/plots/top-50-matplotlib-visualizations-the-master-plots-python/
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
2、导入并查看数据
# import dataset
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/mtcars.csv')
print(df.head(5))
print(df.corr().head(5))
# method 参数默认计算 pearson相关系数,其它还可以计算“spearman”,"kendall"相关系数
3、绘制相关系数热图
# Heatmap plot
plt.figure(figsize = (12,10),dpi = 80)
sns.heatmap(df.corr(),xticklabels = df.corr().columns, yticklabels = df.corr().columns,
cmap = "RdYlGn",center = 0, annot = True)
# Decoration
plt.title('Correlogram of mtcars',fontsize = 22)
plt.xticks(fontsize = 12)
plt.yticks(fontsize = 12)
plt.show()
结果如图:
4、绘制聚类热图
clustermap
plt.figure(figsize = (12,10),dpi = 80)
sns.clustermap(df.corr(),xticklabels = df.corr().columns, yticklabels = df.corr().columns,
cmap = “RdYlGn”,center = 0)
显示如下:
视频中李博士参考的帖子也是学习Python绘图的极佳资料: Top 50 matplotlib Visualizations – The Master Plots (with full python code)