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

This gallery example demonstrates creating plots with multiple traces and advanced styling options using Plotly.cpp. It showcases different line styles, colors, and mathematical functions plotted together with custom legend configuration.

Features demonstrated:

  • Multiple trace plotting with different mathematical functions
  • Custom line styling (solid, dashed lines with different colors)
  • Automatic and manual color assignment for traces
  • Legend configuration and trace naming
  • Axis range control and custom plot dimensions
  • Mathematical function visualization (sine, logarithmic, constant functions)

The plot displays three mathematical relationships:

  • A sine wave function showing periodic behavior
  • A logarithmic growth curve demonstrating non-linear scaling
  • A constant horizontal line for reference comparison
Multi-Trace Mathematical Functions Plot
#include <cmath>
#include <vector>
auto main(int argc, char *argv[]) -> int {
// Parse command line arguments
auto args = parseGalleryArgs(argc, argv);
fig.openBrowser(args.headless);
// Prepare data - equivalent to matplotlib example
int n = 5000;
std::vector<double> x(n), y(n), z(n), w(n, 2.0);
for (int i = 0; i < n; ++i) {
x[i] = i * i;
y[i] = std::sin(2 * M_PI * i / 360.0);
z[i] = std::log(i + 1); // +1 to avoid log(0)
}
// Create traces equivalent to matplotlib plots
// First trace: plot(x, y) - automatically colored line
plotly::Object trace1 = {{"x", x},
{"y", y},
{"type", "scatter"},
{"mode", "lines"},
{"name", "sin(2πi/360)"}};
// Second trace: plot(x, w, "r--") - red dashed line
plotly::Object trace2 = {
{"x", x},
{"y", w},
{"type", "scatter"},
{"mode", "lines"},
{"line", plotly::Object{{"color", "red"}, {"dash", "dash"}}},
{"name", "constant line (y=2)"}};
// Third trace: named_plot("log(x)", x, z) - named line for legend
plotly::Object trace3 = {{"x", x},
{"y", z},
{"type", "scatter"},
{"mode", "lines"},
{"name", "log(x)"}};
// Layout configuration equivalent to matplotlib settings
plotly::Object layout = {
{"title", {{"text", "Sample figure"}}},
{"xaxis",
{
{"title", {{"text", "X values"}}},
{"range", plotly::Array{0, 1000000}} // xlim(0, 1000*1000)
}},
{"yaxis", {{"title", {{"text", "Y values"}}}}},
{"showlegend", true}, // Enable legend
{"width", 1200}, // Set figure size
{"height", 780}};
// Create the plot with all traces
std::vector<plotly::Object> data = {trace1, trace2, trace3};
fig.newPlot(data, layout);
if (!args.headless) {
fig.waitClose();
} else {
// Save the image equivalent to plt::save("./basic.png")
plotly::Object imageOpts = {{"format", "png"},
{"width", 1200},
{"height", 780},
{"filename", "multi_trace_styling"}};
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.