import plotly.graph_objects as go
import pandas as pd

# 示例数据:能源流动(简化版)
data = [
    {"source": "Nuclear", "target": "Thermal generation", "value": 40},
    {"source": "UK land-based bioenergy", "target": "Thermal generation", "value": 15},
    {"source": "Agricultural waste bio-conversion", "target": "Solid", "value": 5},
    {"source": "Other waste", "target": "Solid", "value": 3},
    {"source": "Marine algae", "target": "Solid", "value": 2},
    {"source": "Biomass imports", "target": "Solid", "value": 8},
    {"source": "Coal reserves", "target": "Coal", "value": 10},
    {"source": "Coal imports", "target": "Coal", "value": 12},
    {"source": "Gas reserves", "target": "Gas", "value": 20},
    {"source": "Gas imports", "target": "Gas", "value": 25},
    {"source": "Wind", "target": "Electricity grid", "value": 30},
    {"source": "Wave", "target": "Electricity grid", "value": 2},
    {"source": "Geothermal", "target": "Electricity grid", "value": 1},
    {"source": "Hydro", "target": "Electricity grid", "value": 5},
    {"source": "Tidal", "target": "Electricity grid", "value": 1},
    {"source": "Solar PV", "target": "Electricity grid", "value": 15},
    {"source": "Solar Thermal", "target": "Thermal generation", "value": 6},
    {"source": "Pumped heat", "target": "Electricity grid", "value": 4},
    {"source": "Oil imports", "target": "Oil", "value": 20},
    {"source": "Oil reserves", "target": "Oil", "value": 5},
    {"source": "Biofuel imports", "target": "Liquid", "value": 8},
    {"source": "Solid", "target": "Thermal generation", "value": 30},
    {"source": "Gas", "target": "Thermal generation", "value": 45},
    {"source": "Coal", "target": "Thermal generation", "value": 22},
    {"source": "Oil", "target": "Liquid", "value": 25},
    {"source": "Thermal generation", "target": "Electricity grid", "value": 70},
    {"source": "Thermal generation", "target": "District heating", "value": 25},
    {"source": "Thermal generation", "target": "Losses", "value": 15},
    {"source": "Electricity grid", "target": "Lighting & appliances - homes", "value": 15},
    {"source": "Electricity grid", "target": "Lighting & appliances - comm", "value": 10},
    {"source": "Electricity grid", "target": "Industry", "value": 12},
    {"source": "Electricity grid", "target": "H2 conversion", "value": 5},
    {"source": "Electricity grid", "target": "Over generation/exports", "value": 8},
    {"source": "District heating", "target": "Heating and cooling - homes", "value": 18},
    {"source": "District heating", "target": "Heating and cooling - comm", "value": 7},
    {"source": "H2 conversion", "target": "H2", "value": 5},
    {"source": "H2", "target": "Industry", "value": 3},
    {"source": "H2", "target": "Road transport", "value": 2},
    {"source": "Liquid", "target": "Road transport", "value": 15},
    {"source": "Liquid", "target": "International aviation", "value": 5},
    {"source": "Liquid", "target": "International shipping", "value": 8},
    {"source": "Liquid", "target": "National navigation", "value": 4},
    {"source": "Liquid", "target": "Domestic aviation", "value": 3},
    {"source": "Industry", "target": "Agriculture", "value": 10},
    {"source": "Industry", "target": "Rail transport", "value": 4},
]

# 转换为 DataFrame
df = pd.DataFrame(data)

# 获取所有唯一节点(合并 source 和 target)
nodes = sorted(set(df['source'].tolist() + df['target'].tolist()))

# 创建节点名称到索引的映射
node_to_index = {node: i for i, node in enumerate(nodes)}

# 定义节点颜色(确保长度 ≥ 节点数)
base_colors = [
    '#2E8B57', '#9ACD32', '#FFA500', '#FF6347', '#4169E1', '#FFD700',
    '#DC143C', '#87CEEB', '#FFB6C1', '#FF69B4', '#98FB98', '#FAFAD2',
    '#FFE4B5', '#FFDAB9', '#9370DB', '#B22222', '#CD5C5C', '#228B22',
    '#4682B4', '#DAA520', '#5F9EA0', '#7FFF00', '#6A5ACD', '#FF4500'
]
# 如果节点数超过 base_colors 长度,循环使用
node_colors = [base_colors[i % len(base_colors)] for i in range(len(nodes))]

# 构建链接数据,并为每个 link 设置颜色 = source 节点颜色
links = {
    'source': [node_to_index[src] for src in df['source']],
    'target': [node_to_index[tgt] for tgt in df['target']],
    'value': df['value'].tolist(),
    'color': [node_colors[node_to_index[src]] for src in df['source']]  # 关键修改:link 颜色 = source 节点颜色
}

# 创建 Sankey 图
fig = go.Figure(
    data=go.Sankey(
        node=dict(
            pad=15,
            thickness=20,
            line=dict(color="black", width=0.5),
            label=nodes,
            color=node_colors  # 节点颜色
        ),
        link=dict(
            source=links['source'],
            target=links['target'],
            value=links['value'],
            color=links['color']  # 流向颜色 = source 节点颜色
        )
    )
)

# 更新布局(科研论文风格)
fig.update_layout(
    title_text="Energy Flow Sankey Diagram (Link Colors Match Source Nodes)",
    font_size=12,
    width=1200,
    height=800,
    margin=dict(l=100, r=100, t=60, b=50),
    paper_bgcolor='white',
    plot_bgcolor='white',
    title_font=dict(size=16, family="Arial"),
    hovermode='closest'
)

# 显示图表
fig.show()

# 可选:导出为高分辨率图像(需安装 kaleido)
# fig.write_image("sankey_source_colored.png", scale=2)
# fig.write_image("sankey_source_colored.svg")

在这里插入图片描述

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐