You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Seaborn绘制指定轴二元热力图的技术咨询

如何创建二元热力图(0/1值,Criteria为Y轴)

针对你的需求,这里提供用Python的seaborn和pandas实现的方案,专门适配你的0/1二元数据,把Criteria作为Y轴,其余指标作为X轴:

步骤1:导入依赖库

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from io import StringIO

步骤2:读取并整理数据

这里用你提供的样本数据演示,实际使用时替换为读取本地文件(pd.read_csv("your_file.csv")):

# 样本数据
data_str = """Criteria,Robustness,Stability,Flexibility,Resourcefulness,Coordination Capacitiy,Redundancy,Diversity,Foresight Capacity,Independence,Connectivity & Interdependence,Collaboration Capacity,Agility,Adaptability,Self-Organization,Creativity & Innovation,Efficiency,Equity
Ecosystem Monitoring & Protection,0,1,1,0,1,0,0,1,1,0,1,0,1,1,0,1,0
Using local and native material and species ,1,0,1,0,1,0,1,1,0,1,0,1,0,1,0,0,1
Erosion protection,0,1,0,1,0,1,0,1,0,1,1,0,1,0,1,0,1
Protection of wetlands and watersheds,1,0,1,0,1,0,1,1,0,1,0,1,0,1,0,1,1
Availability and accessibility of resources,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1
Reduction of environmental impacts,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,1,1
Quality of resources,1,0,1,1,1,0,1,0,0,1,0,1,0,1,0,1,0
Biodiversity and wildlife conservation,1,1,0,0,1,0,0,1,1,1,1,1,0,0,1,0,1
"Material and resource management (production, consumption, conservation, recycling etc)",1,1,0,1,1,0,1,1,1,0,1,1,0,1,1,0,1
"""

# 读取数据,将Criteria设为索引(作为Y轴标签)
df = pd.read_csv(StringIO(data_str), index_col="Criteria")

步骤3:绘制二元热力图

# 设置自定义颜色:0对应浅灰,1对应深绿(可自行调整)
cmap = sns.color_palette(["#e0e0e0", "#2e7d32"], as_cmap=True)

# 创建热力图
plt.figure(figsize=(14, 8))
ax = sns.heatmap(df, 
                 annot=True,  # 显示单元格内的0/1数值
                 fmt="d",     # 以整数格式显示
                 cmap=cmap,   # 自定义颜色映射
                 cbar=False,  # 关闭颜色条(因为只有0/1两种值,可选保留)
                 yticklabels=True,
                 xticklabels=True)

# 调整X轴标签角度,避免重叠
plt.xticks(rotation=45, ha="right")

# 添加标题
plt.title("Criteria vs. Indicators Binary Heatmap")

# 调整布局,防止标签被截断
plt.tight_layout()

# 显示图形
plt.show()

关键说明

  • 把Criteria设为数据框的索引后,seaborn.heatmap会自动将其作为Y轴标签,其余列作为X轴标签,完全匹配你的需求。
  • 自定义颜色映射cmap的列表顺序对应0和1的颜色:列表第一个元素是0的颜色,第二个是1的颜色,可根据需求替换为任意十六进制颜色码或颜色名称。
  • annot=True和fmt="d"确保单元格内显示原始的0/1数值,方便查看。

内容的提问来源于stack exchange,提问作者JamesArthur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 01:13:59