Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 55 additions & 63 deletions plots/sankey-basic/implementations/python/plotly.py
Original file line number Diff line number Diff line change
@@ -1,91 +1,83 @@
""" pyplots.ai
""" anyplot.ai
sankey-basic: Basic Sankey Diagram
Library: plotly 6.5.0 | Python 3.13.11
Quality: 93/100 | Created: 2025-12-23
Library: plotly 6.7.0 | Python 3.13.13
Quality: 90/100 | Updated: 2026-04-30
"""

import os

import plotly.graph_objects as go


# Data - Energy flow from sources to end-use sectors (in TWh)
# Theme tokens
THEME = os.getenv("ANYPLOT_THEME", "light")
PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17"
INK = "#1A1A17" if THEME == "light" else "#F0EFE8"
INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0"

# Okabe-Ito palette for source nodes (positions 1-4)
OKABE_ITO = ["#009E73", "#D55E00", "#0072B2", "#CC79A7"]
SOURCE_RGBA = ["rgba(0,158,115,0.4)", "rgba(213,94,0,0.4)", "rgba(0,114,178,0.4)", "rgba(204,121,167,0.4)"]

# Data - Energy flow from sources to sectors (TWh)
sources = ["Coal", "Natural Gas", "Nuclear", "Renewables"]
targets = ["Residential", "Commercial", "Industrial", "Transportation"]

# Node labels (sources first, then targets)
labels = sources + targets

# Define flows: (source_index, target_index, value)
# Indices: Coal=0, Gas=1, Nuclear=2, Renewables=3
# Residential=4, Commercial=5, Industrial=6, Transportation=7
flows = [
# Coal flows
(0, 4, 5), # Coal -> Residential
(0, 5, 8), # Coal -> Commercial
(0, 6, 25), # Coal -> Industrial
# Natural Gas flows
(1, 4, 22), # Gas -> Residential
(1, 5, 18), # Gas -> Commercial
(1, 6, 15), # Gas -> Industrial
(1, 7, 3), # Gas -> Transportation
# Nuclear flows
(2, 4, 12), # Nuclear -> Residential
(2, 5, 10), # Nuclear -> Commercial
(2, 6, 8), # Nuclear -> Industrial
# Renewables flows
(3, 4, 8), # Renewables -> Residential
(3, 5, 6), # Renewables -> Commercial
(3, 6, 5), # Renewables -> Industrial
(3, 7, 4), # Renewables -> Transportation
(0, 4, 5),
(0, 5, 8),
(0, 6, 25),
(1, 4, 22),
(1, 5, 18),
(1, 6, 15),
(1, 7, 3),
(2, 4, 12),
(2, 5, 10),
(2, 6, 8),
(3, 4, 8),
(3, 5, 6),
(3, 6, 5),
(3, 7, 4),
]

source_indices = [f[0] for f in flows]
target_indices = [f[1] for f in flows]
values = [f[2] for f in flows]

# Colors for source nodes (Python Blue variations and Yellow)
node_colors = [
"#306998", # Coal - Python Blue
"#4A90C2", # Natural Gas - Light Blue
"#FFD43B", # Nuclear - Python Yellow
"#7AB648", # Renewables - Green
"#8FA8BD", # Residential
"#A3B8CC", # Commercial
"#B8C9DB", # Industrial
"#CCD9E8", # Transportation
]

# Link colors with transparency (based on source)
link_colors = [
"rgba(48, 105, 152, 0.5)"
if s == 0
else "rgba(74, 144, 194, 0.5)"
if s == 1
else "rgba(255, 212, 59, 0.5)"
if s == 2
else "rgba(122, 182, 72, 0.5)"
for s in source_indices
]
# Source nodes use Okabe-Ito colors; target nodes use INK_SOFT
node_colors = OKABE_ITO + [INK_SOFT] * 4
link_colors = [SOURCE_RGBA[s] for s in source_indices]

# Create Sankey diagram
# Plot
fig = go.Figure(
data=[
go.Sankey(
node=dict(pad=25, thickness=35, line=dict(color="white", width=2), label=labels, color=node_colors),
link=dict(source=source_indices, target=target_indices, value=values, color=link_colors),
node={
"pad": 25,
"thickness": 35,
"line": {"color": PAGE_BG, "width": 1},
"label": labels,
"color": node_colors,
},
link={"source": source_indices, "target": target_indices, "value": values, "color": link_colors},
)
]
)

# Update layout for 4800x2700 px output
fig.update_layout(
title=dict(
text="Energy Distribution · sankey-basic · plotly · pyplots.ai", font=dict(size=36), x=0.5, xanchor="center"
),
font=dict(size=22),
template="plotly_white",
margin=dict(l=80, r=80, t=120, b=60),
title={
"text": "Energy Distribution · sankey-basic · plotly · anyplot.ai",
"font": {"size": 28, "color": INK},
"x": 0.5,
"xanchor": "center",
},
paper_bgcolor=PAGE_BG,
plot_bgcolor=PAGE_BG,
font={"size": 22, "color": INK},
margin={"l": 80, "r": 80, "t": 120, "b": 60},
)

# Save outputs
fig.write_image("plot.png", width=1600, height=900, scale=3)
fig.write_html("plot.html")
# Save
fig.write_image(f"plot-{THEME}.png", width=1600, height=900, scale=3)
fig.write_html(f"plot-{THEME}.html", include_plotlyjs="cdn")
Loading
Loading