-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgallery_2x2_subplots.cpp
More file actions
108 lines (96 loc) 路 3.47 KB
/
Copy pathgallery_2x2_subplots.cpp
File metadata and controls
108 lines (96 loc) 路 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* @file gallery_2x2_subplots.cpp
* @brief 2x2 Subplots - Multiple Plot Layout Example
* @author plotly.cpp contributors
* @date 2025
*
* @example gallery_2x2_subplots.cpp
*
* # 2x2 Subplots Example
*
* This example demonstrates how to create multiple subplots in a 2x2 grid
* layout, showing different sine wave phases in each subplot.
*
* ## What You'll Learn
* - Creating subplot layouts with multiple traces
* - Positioning plots in grid arrangements
* - Generating mathematical data for visualization
* - Working with phase-shifted sine waves
*
* ## Sample Output
* The example creates a 2x2 grid showing sine waves with different phases:
* - Top-left: sin(t) (phase 0)
* - Top-right: sin(t + 蟺/4) (phase 蟺/4)
* - Bottom-left: sin(t + 蟺/2) (phase 蟺/2)
* - Bottom-right: sin(t + 3蟺/4) (phase 3蟺/4)
*
* @image html 2x2_subplots.png "2x2 Subplots Example Output"
*
* @see plotly::Figure For the main plotting interface
*/
#include "plotly/plotly.hpp"
#include "utils/arg_parser.hpp"
#include <cmath>
#include <vector>
auto main(int argc, char *argv[]) -> int {
// Parse command line arguments
auto args = parseGalleryArgs(argc, argv);
plotly::Figure fig;
fig.openBrowser(args.headless);
// Generate x values for all traces
std::vector<double> x;
for (double t = 0; t < 4 * M_PI; t += 0.1) {
x.push_back(t);
}
// Generate y values for each trace with different phases
std::vector<double> y1, y2, y3, y4;
for (double t : x) {
y1.push_back(std::sin(t)); // Phase 0
y2.push_back(std::sin(t + M_PI / 4)); // Phase 蟺/4
y3.push_back(std::sin(t + M_PI / 2)); // Phase 蟺/2
y4.push_back(std::sin(t + 3 * M_PI / 4)); // Phase 3蟺/4
}
plotly::Object trace1 = {{"x", x},
{"y", y1},
{"type", "scatter"},
{"mode", "lines"},
{"name", "sin(t)"}};
plotly::Object trace2 = {{"x", x},
{"y", y2},
{"xaxis", "x2"},
{"yaxis", "y2"},
{"type", "scatter"},
{"mode", "lines"},
{"name", "sin(t + 蟺/4)"}};
plotly::Object trace3 = {{"x", x},
{"y", y3},
{"xaxis", "x3"},
{"yaxis", "y3"},
{"type", "scatter"},
{"mode", "lines"},
{"name", "sin(t + 蟺/2)"}};
plotly::Object trace4 = {{"x", x},
{"y", y4},
{"xaxis", "x4"},
{"yaxis", "y4"},
{"type", "scatter"},
{"mode", "lines"},
{"name", "sin(t + 3蟺/4)"}};
plotly::Array data = {trace1, trace2, trace3, trace4};
plotly::Object layout = {
{"title", {{"text", "2x2 Subplot Grid - Phase-shifted Sine Waves"}}},
{"grid", {{"rows", 2}, {"columns", 2}, {"pattern", "independent"}}},
{"showlegend", false}};
fig.newPlot(data, layout);
if (!args.headless) {
fig.waitClose();
} else {
// Save image instead of opening browser
plotly::Object imageOpts = {{"format", "png"},
{"width", 800},
{"height", 600},
{"filename", "2x2_subplots"}};
fig.downloadImage(imageOpts);
}
return 0;
}