|
1 | | -""" pyplots.ai |
| 1 | +""" anyplot.ai |
2 | 2 | sankey-basic: Basic Sankey Diagram |
3 | | -Library: highcharts unknown | Python 3.13.11 |
4 | | -Quality: 91/100 | Created: 2025-12-23 |
| 3 | +Library: highcharts unknown | Python 3.13.13 |
| 4 | +Quality: 88/100 | Updated: 2026-04-30 |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import os |
7 | 8 | import tempfile |
8 | 9 | import time |
9 | 10 | import urllib.request |
10 | 11 | from pathlib import Path |
11 | 12 |
|
12 | | -import numpy as np |
13 | 13 | from highcharts_core.chart import Chart |
14 | 14 | from highcharts_core.options import HighchartsOptions |
15 | | -from PIL import Image |
16 | 15 | from selenium import webdriver |
17 | 16 | from selenium.webdriver.chrome.options import Options |
18 | 17 |
|
19 | 18 |
|
20 | | -# Reproducibility |
21 | | -np.random.seed(42) |
| 19 | +# Theme tokens |
| 20 | +THEME = os.getenv("ANYPLOT_THEME", "light") |
| 21 | +PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17" |
| 22 | +INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
| 23 | +INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0" |
22 | 24 |
|
23 | | -# Data - Energy flow from sources to sectors (values in TWh - Terawatt-hours) |
24 | | -# Format: [source, target, value] |
| 25 | +# Data - U.S. Energy flow from sources to sectors (values in TWh) |
25 | 26 | flows = [ |
26 | | - # Coal flows |
27 | 27 | ["Coal", "Electricity", 150], |
28 | 28 | ["Coal", "Industrial", 80], |
29 | | - # Natural Gas flows |
30 | 29 | ["Natural Gas", "Electricity", 120], |
31 | 30 | ["Natural Gas", "Residential", 90], |
32 | 31 | ["Natural Gas", "Commercial", 60], |
33 | 32 | ["Natural Gas", "Industrial", 50], |
34 | | - # Nuclear flows |
35 | 33 | ["Nuclear", "Electricity", 200], |
36 | | - # Petroleum flows |
37 | 34 | ["Petroleum", "Transportation", 280], |
38 | 35 | ["Petroleum", "Industrial", 70], |
39 | 36 | ["Petroleum", "Residential", 30], |
40 | | - # Renewable flows |
41 | 37 | ["Renewable", "Electricity", 100], |
42 | 38 | ["Renewable", "Transportation", 20], |
43 | | - # Electricity flows to end uses |
44 | 39 | ["Electricity", "Residential", 180], |
45 | 40 | ["Electricity", "Commercial", 160], |
46 | 41 | ["Electricity", "Industrial", 140], |
47 | 42 | ] |
48 | 43 |
|
49 | | -# Collect unique nodes |
50 | | -nodes_set = set() |
51 | | -for source, target, _ in flows: |
52 | | - nodes_set.add(source) |
53 | | - nodes_set.add(target) |
54 | | -nodes = list(nodes_set) |
| 44 | +# Node colors - Okabe-Ito palette (canonical order for source nodes) |
| 45 | +OKABE_ITO = ["#009E73", "#D55E00", "#0072B2", "#CC79A7", "#E69F00", "#56B4E9", "#F0E442"] |
55 | 46 |
|
56 | | -# Colorblind-safe colors for nodes - all dark enough for white text contrast |
57 | 47 | node_colors = { |
58 | | - # Sources (energy sources) - dark tones for white text readability |
59 | | - "Coal": "#1A3A5C", # Dark Blue |
60 | | - "Natural Gas": "#6B4E12", # Darker Goldenrod (darkened for contrast) |
61 | | - "Nuclear": "#5B2E8F", # Dark Purple |
62 | | - "Petroleum": "#0A6B78", # Dark Cyan |
63 | | - "Renewable": "#155415", # Dark Green |
64 | | - # Intermediate - dark for contrast |
65 | | - "Electricity": "#4D2A22", # Dark Brown |
66 | | - # End uses - darker shades for white text visibility |
67 | | - "Residential": "#8B3A6B", # Dark Rose |
68 | | - "Commercial": "#3A3A3A", # Darker Gray |
69 | | - "Industrial": "#5B5C0A", # Dark Olive |
70 | | - "Transportation": "#994D00", # Darker Orange |
| 48 | + "Coal": OKABE_ITO[0], |
| 49 | + "Natural Gas": OKABE_ITO[1], |
| 50 | + "Nuclear": OKABE_ITO[2], |
| 51 | + "Petroleum": OKABE_ITO[3], |
| 52 | + "Renewable": OKABE_ITO[4], |
| 53 | + "Electricity": OKABE_ITO[5], |
| 54 | + "Transportation": OKABE_ITO[6], |
| 55 | + "Industrial": OKABE_ITO[0], |
| 56 | + "Residential": OKABE_ITO[1], |
| 57 | + "Commercial": OKABE_ITO[2], |
71 | 58 | } |
72 | 59 |
|
73 | | -# Create nodes data with colors |
74 | | -nodes_data = [{"id": node, "name": node, "color": node_colors.get(node, "#306998")} for node in nodes] |
| 60 | +nodes_set = set() |
| 61 | +for source, target, _ in flows: |
| 62 | + nodes_set.add(source) |
| 63 | + nodes_set.add(target) |
75 | 64 |
|
76 | | -# Create links data |
| 65 | +nodes_data = [{"id": node, "name": node, "color": node_colors.get(node, OKABE_ITO[0])} for node in nodes_set] |
77 | 66 | links_data = [{"from": source, "to": target, "weight": value} for source, target, value in flows] |
78 | 67 |
|
79 | | -# Create chart |
| 68 | +# Chart |
80 | 69 | chart = Chart(container="container") |
81 | 70 | chart.options = HighchartsOptions() |
82 | 71 |
|
83 | | -# Chart configuration with margins to prevent label cutoff at edges |
84 | 72 | chart.options.chart = { |
85 | 73 | "type": "sankey", |
86 | 74 | "width": 4800, |
87 | 75 | "height": 2700, |
88 | | - "backgroundColor": "#ffffff", |
| 76 | + "backgroundColor": PAGE_BG, |
89 | 77 | "marginLeft": 180, |
90 | 78 | "marginRight": 180, |
91 | 79 | "marginTop": 160, |
92 | | - "marginBottom": 80, |
| 80 | + "marginBottom": 160, |
93 | 81 | } |
94 | 82 |
|
95 | | -# Title |
96 | 83 | chart.options.title = { |
97 | | - "text": "sankey-basic · highcharts · pyplots.ai", |
98 | | - "style": {"fontSize": "64px", "fontWeight": "bold", "color": "#333333"}, |
| 84 | + "text": "sankey-basic · highcharts · anyplot.ai", |
| 85 | + "style": {"fontSize": "64px", "fontWeight": "bold", "color": INK}, |
99 | 86 | } |
100 | 87 |
|
101 | | -# Subtitle with units info |
102 | | -chart.options.subtitle = {"text": "U.S. Energy Flow (values in TWh)", "style": {"fontSize": "40px", "color": "#666666"}} |
| 88 | +chart.options.subtitle = {"text": "U.S. Energy Flow (values in TWh)", "style": {"fontSize": "40px", "color": INK_SOFT}} |
103 | 89 |
|
104 | | -# Tooltip with units |
105 | 90 | chart.options.tooltip = { |
106 | 91 | "style": {"fontSize": "36px"}, |
107 | 92 | "nodeFormat": "{point.name}: {point.sum} TWh", |
108 | 93 | "pointFormat": "{point.fromNode.name} → {point.toNode.name}: {point.weight} TWh", |
109 | 94 | } |
110 | 95 |
|
111 | | -# Sankey series configuration |
112 | | -series_config = { |
113 | | - "type": "sankey", |
114 | | - "name": "Energy Flow", |
115 | | - "keys": ["from", "to", "weight"], |
116 | | - "nodes": nodes_data, |
117 | | - "data": links_data, |
118 | | - "dataLabels": { |
119 | | - "enabled": True, |
120 | | - "style": {"fontSize": "36px", "fontWeight": "bold", "color": "#FFFFFF", "textOutline": "3px #333333"}, |
121 | | - "nodeFormat": "{point.name}", |
122 | | - }, |
123 | | - "nodeWidth": 50, |
124 | | - "nodePadding": 35, |
125 | | - "linkOpacity": 0.5, |
126 | | - "curveFactor": 0.5, |
127 | | - "colorByPoint": True, |
128 | | - "linkColorMode": "from", |
129 | | -} |
130 | | - |
131 | | -chart.options.series = [series_config] |
| 96 | +chart.options.series = [ |
| 97 | + { |
| 98 | + "type": "sankey", |
| 99 | + "name": "Energy Flow", |
| 100 | + "keys": ["from", "to", "weight"], |
| 101 | + "nodes": nodes_data, |
| 102 | + "data": links_data, |
| 103 | + "dataLabels": { |
| 104 | + "enabled": True, |
| 105 | + "style": {"fontSize": "36px", "fontWeight": "bold", "color": "#FFFFFF", "textOutline": "3px #333333"}, |
| 106 | + "nodeFormat": "{point.name}", |
| 107 | + }, |
| 108 | + "nodeWidth": 50, |
| 109 | + "nodePadding": 35, |
| 110 | + "linkOpacity": 0.5, |
| 111 | + "curveFactor": 0.5, |
| 112 | + "colorByPoint": True, |
| 113 | + "linkColorMode": "from", |
| 114 | + } |
| 115 | +] |
132 | 116 |
|
133 | | -# Disable legend for sankey (nodes are labeled) |
134 | 117 | chart.options.legend = {"enabled": False} |
135 | | - |
136 | | -# Disable credits |
137 | 118 | chart.options.credits = {"enabled": False} |
138 | 119 |
|
139 | | -# Download Highcharts JS and sankey module |
140 | | -highcharts_url = "https://code.highcharts.com/highcharts.js" |
141 | | -sankey_url = "https://code.highcharts.com/modules/sankey.js" |
142 | | - |
143 | | -with urllib.request.urlopen(highcharts_url, timeout=30) as response: |
| 120 | +# Download Highcharts JS and sankey module inline (required for headless Chrome) |
| 121 | +with urllib.request.urlopen("https://cdn.jsdelivr.net/npm/highcharts@latest/highcharts.js", timeout=30) as response: |
144 | 122 | highcharts_js = response.read().decode("utf-8") |
145 | 123 |
|
146 | | -with urllib.request.urlopen(sankey_url, timeout=30) as response: |
| 124 | +with urllib.request.urlopen("https://cdn.jsdelivr.net/npm/highcharts@latest/modules/sankey.js", timeout=30) as response: |
147 | 125 | sankey_js = response.read().decode("utf-8") |
148 | 126 |
|
149 | | -# Generate HTML with inline scripts |
150 | 127 | html_str = chart.to_js_literal() |
151 | 128 | html_content = f"""<!DOCTYPE html> |
152 | 129 | <html> |
|
155 | 132 | <script>{highcharts_js}</script> |
156 | 133 | <script>{sankey_js}</script> |
157 | 134 | </head> |
158 | | -<body style="margin:0;"> |
159 | | - <div id="container" style="width: 4800px; height: 2700px;"></div> |
160 | | - <script>{html_str}</script> |
161 | | -</body> |
162 | | -</html>""" |
163 | | - |
164 | | -# Save HTML for interactive version (use CDN for standalone) - fixed dimensions like PNG |
165 | | -standalone_html = f"""<!DOCTYPE html> |
166 | | -<html> |
167 | | -<head> |
168 | | - <meta charset="utf-8"> |
169 | | - <script src="https://code.highcharts.com/highcharts.js"></script> |
170 | | - <script src="https://code.highcharts.com/modules/sankey.js"></script> |
171 | | -</head> |
172 | | -<body style="margin:0; overflow:auto;"> |
| 135 | +<body style="margin:0; background:{PAGE_BG};"> |
173 | 136 | <div id="container" style="width: 4800px; height: 2700px;"></div> |
174 | 137 | <script>{html_str}</script> |
175 | 138 | </body> |
176 | 139 | </html>""" |
177 | 140 |
|
178 | | -with open("plot.html", "w", encoding="utf-8") as f: |
179 | | - f.write(standalone_html) |
| 141 | +# Save HTML artifact |
| 142 | +with open(f"plot-{THEME}.html", "w", encoding="utf-8") as f: |
| 143 | + f.write(html_content) |
180 | 144 |
|
181 | | -# Write temp HTML and take screenshot |
| 145 | +# Screenshot via headless Chrome |
182 | 146 | with tempfile.NamedTemporaryFile(mode="w", suffix=".html", delete=False, encoding="utf-8") as f: |
183 | 147 | f.write(html_content) |
184 | 148 | temp_path = f.name |
|
188 | 152 | chrome_options.add_argument("--no-sandbox") |
189 | 153 | chrome_options.add_argument("--disable-dev-shm-usage") |
190 | 154 | chrome_options.add_argument("--disable-gpu") |
191 | | -chrome_options.add_argument("--window-size=4800,2900") |
| 155 | +chrome_options.add_argument("--window-size=4800,2700") |
192 | 156 |
|
193 | 157 | driver = webdriver.Chrome(options=chrome_options) |
194 | 158 | driver.get(f"file://{temp_path}") |
195 | | -time.sleep(5) # Wait for chart to render |
196 | | -driver.save_screenshot("plot_raw.png") |
| 159 | +time.sleep(5) |
| 160 | +driver.save_screenshot(f"plot-{THEME}.png") |
197 | 161 | driver.quit() |
198 | 162 |
|
199 | | -# Crop to exact 4800x2700 dimensions |
200 | | -img = Image.open("plot_raw.png") |
201 | | -img_cropped = img.crop((0, 0, 4800, 2700)) |
202 | | -img_cropped.save("plot.png") |
203 | | -Path("plot_raw.png").unlink() |
204 | | - |
205 | | -Path(temp_path).unlink() # Clean up temp file |
| 163 | +Path(temp_path).unlink() |
0 commit comments