This gallery example demonstrates creating a comprehensive Sankey diagram using Plotly.cpp to visualize energy flows from sources through sectors to end uses. Sankey diagrams excel at showing flow quantities and relationships in complex systems through proportional link widths.
The diagram provides insights into energy distribution patterns, bottlenecks, and the relative importance of different pathways in the energy system.
#include <string>
#include <vector>
auto main(
int argc,
char *argv[]) ->
int {
std::vector<std::string> nodeLabels = {
"Coal", "Natural Gas", "Nuclear", "Hydro", "Solar/Wind",
"Electricity Generation", "Industrial", "Transportation", "Residential",
"Lighting", "Heating", "Manufacturing", "Transportation"};
std::vector<std::string> nodeColors = {
"#8B4513", "#4169E1", "#FF4500", "#1E90FF", "#32CD32",
"#FFD700", "#FF6347", "#9370DB", "#20B2AA",
"#FFFF99", "#FFA07A", "#DDA0DD", "#98FB98"};
std::vector<int> sources = {
0, 1, 2, 3, 4,
1, 0,
1,
1, 5,
5, 5, 8,
6,
7,
8, 8
};
std::vector<int> targets = {
5, 5, 5, 5, 5,
6, 6,
7,
8, 8,
9, 10, 10,
11,
12,
10, 9
};
std::vector<double> values = {
35, 40, 20, 15, 10,
25, 15,
30,
20, 50,
40, 30, 20,
40,
30,
35, 35
};
std::vector<std::string> linkColors;
for (int source : sources) {
const std::string &baseColor = nodeColors[source];
linkColors.push_back(baseColor + "80");
}
{"type", "sankey"},
{"orientation", "h"},
{"node",
{{"pad", 15},
{"thickness", 20},
{"line", {{"color", "black"}, {"width", 0.5}}},
{"label", nodeLabels},
{"color", nodeColors},
{"hovertemplate", "%{label}<br>Total Flow: %{value}<extra></extra>"}}},
{"link",
{{"source", sources},
{"target", targets},
{"value", values},
{"color", linkColors},
{"hovertemplate",
"Flow: %{source.label} → %{target.label}<br>" +
std::string("Amount: %{value} units<extra></extra>")}}}};
{"title",
{{"text", "Energy Flow Diagram<br>" +
std::string("<sub>From Sources to End Uses</sub>")},
{"font", {{"size", 18}}}}},
{"width", 1100},
{"height", 700},
{"margin", {{"l", 50}, {"r", 50}, {"t", 80}, {"b", 50}}},
{"annotations",
{{{"text", "Flow thickness represents energy quantity"},
{"x", 0.5},
{"y", -0.08},
{"xref", "paper"},
{"yref", "paper"},
{"showarrow", false},
{"font", {{"size", 12}}}}}}};
std::vector<plotly::Object> data = {trace};
if (!args.headless) {
} else {
{"width", 1100},
{"height", 700},
{"filename", "sankey_flow_diagram"}};
}
return 0;
}
auto parseGalleryArgs(int argc, char *argv[]) -> GalleryArgs
Parse command line arguments for gallery examples.
Definition arg_parser.cpp:4
auto main() -> int
Definition gallery_animate_sin_wave.cpp:48
nlohmann::json Object
Definition plotly.hpp:26
Public Plotly C++ API header.