Plotly.cpp 0.1.0
A C++ plotting library for expressive, interactive, real-time & streaming data visualization
Loading...
Searching...
No Matches
gallery_3d_surface.cpp

3D Surface Plot Example

This example demonstrates how to create three-dimensional surface plots using plotly.cpp, visualizing mathematical functions in 3D space.

What You'll Learn

  • Creating 3D surface plots from mathematical functions
  • Generating 2D grid data for surface visualization
  • Working with colorscales and surface styling
  • Mathematical function visualization techniques

Sample Output

The example creates a 3D surface plot of the function:

z = sin(√(x² + y²))

This creates a ripple effect emanating from the center, visualized with the Viridis colorscale for better depth perception.

3D Surface Plot Example Output
See also
plotly::Figure For the main plotting interface
#include <cmath>
#include <vector>
auto main(int argc, char *argv[]) -> int {
// Parse command line arguments
auto args = parseGalleryArgs(argc, argv);
fig.openBrowser(args.headless);
// Generate 3D surface data
int size = 50;
std::vector<std::vector<double>> z(size, std::vector<double>(size));
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
double x = (i - size / 2.0) * 0.2;
double y = (j - size / 2.0) * 0.2;
z[i][j] = std::sin(std::sqrt(x * x + y * y));
}
}
plotly::Object trace = {
{"z", z}, {"type", "surface"}, {"colorscale", "Viridis"}};
if (!args.headless) {
fig.waitClose();
} else {
// Save image instead of opening browser
plotly::Object imageOpts = {{"format", "png"},
{"width", 800},
{"height", 600},
{"filename", "3d_surface"}};
fig.downloadImage(imageOpts);
}
return 0;
}
auto parseGalleryArgs(int argc, char *argv[]) -> GalleryArgs
Parse command line arguments for gallery examples.
Definition arg_parser.cpp:4
Handle for creating and manipulating a Plotly figure.
Definition plotly.hpp:48
void waitClose() const
Wait until the figure is closed (no client connected).
Definition plotly.cpp:406
auto downloadImage(const Object &opts=Object()) -> bool
Download the figure as an image.
Definition plotly.cpp:413
auto newPlot(const Object &data, const Object &layout=Object(), const Object &config=Object()) -> bool
Create and render a new plot.
Definition plotly.cpp:408
auto openBrowser(bool headless=false) -> bool
Open the figure in the browser.
Definition plotly.cpp:529
auto main() -> int
Definition gallery_animate_sin_wave.cpp:48
std::vector< Object > Array
Definition plotly.hpp:27
nlohmann::json Object
Definition plotly.hpp:26
Public Plotly C++ API header.