Python绘制热力图代码

她笑中藏泪花
2月13日发布

Python绘制热力图代码

import matplotlib.pyplot as plt  
import seaborn as sns  
import numpy as np  
import pandas as pd  
import matplotlib.font_manager as fm  
  
# 创建数据  
data = {  
    "绩效期望": [1, 0.712, 0.687, 0.651, 0.451, 0.743, 0.646, 0.696, 0.684],  
    "努力期望": [0.712, 1, 0.745, 0.731, 0.499, 0.701, 0.618, 0.745, 0.665],  
    "促进因素": [0.687, 0.745, 1, 0.817, 0.544, 0.781, 0.766, 0.713, 0.801],  
    "社会影响": [0.651, 0.731, 0.817, 1, 0.602, 0.757, 0.691, 0.744, 0.759],  
    "感知风险": [0.451, 0.499, 0.544, 0.602, 1, 0.57, 0.593, 0.589, 0.576],  
    "享乐动机": [0.743, 0.701, 0.781, 0.757, 0.57, 1, 0.752, 0.806, 0.782],  
    "价格价值": [0.646, 0.618, 0.766, 0.691, 0.593, 0.752, 1, 0.716, 0.834],  
    "个体创新": [0.696, 0.745, 0.713, 0.744, 0.589, 0.806, 0.716, 1, 0.779],  
    "使用意愿": [0.684, 0.665, 0.801, 0.759, 0.576, 0.782, 0.834, 0.779, 1]  
}  
  
# 将数据转换为DataFrame  
df = pd.DataFrame(data, index=["绩效期望", "努力期望", "促进因素", "社会影响", "感知风险", "享乐动机", "价格价值", "个体创新", "使用意愿"])  
  
# 设置字体  
plt.rcParams['font.family'] = 'Microsoft YaHei'  
  
# 设置绘图的大小  
plt.figure(figsize=(10, 8))  
  
# 绘制热力图  
sns.heatmap(df, annot=True, cmap='coolwarm', center=0, linewidths=0.5, fmt=".3f", cbar_kws={'label': '相关性'})  
  
# 设置标题  
plt.title("相关性分析热力图")  
  
# 显示热力图  
plt.show()
© 版权声明
THE END
喜欢就支持一下吧
点赞 0 分享 收藏
评论 抢沙发
OωO
取消