GregoireUhlrich / cpp-plotting-library

Scientific plotting library for C++
GNU General Public License v3.0
1 stars 0 forks source link

Interface proposal for unified plots #18

Open GregoireUhlrich opened 2 years ago

GregoireUhlrich commented 2 years ago

Here is a user interface proposal to unify plots between different types (at least 2D plots). A unique object Plot2D is created given x and y data. The plot style (line, hist etc) must be given as well as the relevant configuration (optionally). Similarly, errors can be given independently for any plot style.

int main {

    // ...
    // Get a Subplot
    cpt::Subplot &subplot = figure.get_subplot();

    // Define data for line plot, including an error along y
    auto x    = cpt::linspace(0, 10, 100);
    auto y    = cpt::cos(x);
    auto yerr = cpt::sin(x) / 2.f;

    // Line plot
    Plot2D line_plot = subplot.plot(
        cpt::Style::Line,   // Plot style
        x, y,               // Data 
        {.line_width = .2f} // Config
    );
    line.set_yerr(yerr);    // Error along y

    // Define data for some histogram
    cpt::RandomDevice rand;
    auto rand_data = rand.rand_real(100, 0.f, 10.f); // 100 random numbers between 0 and 10

    // Calculate the histogram separately (no link to plot / graphics here)
    cpt::Histogram hist(
        rand_data, 
        {.nbins=10, .vmin=0.f, .vmax=10.f} // Config for the hist calculation
    );

    // Hist plot
    Plot2D hist_plot = subplot.plot(
        cpt::Style::Hist,            // Plot style
        hist.x, hist.y,              // Data 
        {.color = cpt::Color::Green} // Config
    );
    hist_plot.set_yerr(hist.get_ystd()); // Error along y
                                         // Also calculated by the Histogram class.
                                         // Several statistical error calculations
                                         // could be provided by this class.
}
aureliencarle commented 2 years ago

Ok let's do this