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

This gallery example demonstrates creating a basic sine wave plot using Plotly.cpp. It showcases fundamental plotting capabilities with mathematical function visualization over a continuous domain.

Features demonstrated:

  • Mathematical function plotting using scatter trace type
  • Continuous curve generation with linspace utility
  • Basic trigonometric function visualization (sine wave)
  • Simple plot creation with minimal configuration
  • Standard domain range (-2π to 2π) for complete wave cycles
  • High-resolution curve with 200 data points for smooth visualization

Mathematical concepts:

  • Sine function: y = sin(x)
  • Domain: [-2π, 2π] covering two complete oscillation cycles
  • Amplitude: 1.0 (standard unit amplitude)
  • Period: 2π (standard sine wave period)

This example serves as an introduction to basic mathematical function plotting and demonstrates the ease of creating smooth curves with Plotly.cpp's scatter trace functionality.

Sine Wave Mathematical Function
#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 x values from -2π to 2π
auto x = linspace(-2 * M_PI, 2 * M_PI, 200);
// Calculate sin values
std::vector<double> y;
y.reserve(x.size());
for (const auto &xi : x) {
y.push_back(std::sin(xi));
}
// Create scatter trace for sin curve
plotly::Object trace = {
{"x", x},
{"y", y},
{"type", "scatter"},
};
// Create the plot
std::vector<plotly::Object> data = {trace};
fig.newPlot(data);
if (!args.headless) {
fig.waitClose();
} else {
// Save image instead of opening browser
plotly::Object imageOpts = {{"format", "png"},
{"width", 800},
{"height", 600},
{"filename", "sin_curve"}};
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
auto linspace(double a, double b, int n) -> std::vector< double >
Create a linearly spaced vector of values between two endpoints.
Definition linspace.cpp:4
nlohmann::json Object
Definition plotly.hpp:26
Public Plotly C++ API header.