Skip to content

Latest commit

 

History

History
189 lines (155 loc) · 4.25 KB

File metadata and controls

189 lines (155 loc) · 4.25 KB

Gnuplot++ quick start

This is a quick start guide, for more detailed usage description, please read the usage guide.

Including Gnuplot++

The first step is to include gnuplot in your project. There are several ways to do this, the simplest is using CMake:

find_package(gnuplotpp REQUIRED)
target_link_libraries(your_target_name PUBLIC gnuplotpp) # or PRIVATE

For other ways to use Gnuplot++ in your project, read the BuildAndInstall guide.

At this point in your code you can type:

#include <gnuplotpp/gnuplotpp.hpp>

// gnuplotpp is in the lc namespace, this line is not really necessary
using namespace lc;

Sending commands:

Let's first create a Gnuplot++ instance:

Gnuplotpp gp;

You can send commands to Gnuplot in this way:

gp << "plot [-pi/2:pi] cos(x),-(sin(x) > sin(x+1) ? sin(x) : sin(x+1))" << std::endl;

and you can create more complex commands:

const auto cmd = R"ashffsd(
plot '-' pt 6 ps 5, '-' pt 3 ps 5
1
3
2
1
EOF
2
5
6
2
3
EOF
)ashffsd";

Remember that gp << std::endl pushes a "\n" and flushes the queue, this means it is the equivalent of "pressing enter" on the keyboard in the Gnuplot terminal.

Generating a plot

Gnuplot++ library provide simple ways to create plots from C++, the simplest form is:

Gnuplotpp gp;

// This creates a plot instance that contains all the necessary data to render the chart
auto myPlot = gnuplot.plot({ 1, 2, 3, 2 });

// actually renders the plot
gp.draw({ myPlot });

Example

#include <iostream>
#include <random>
#include <vector>

#include <gnuplotpp/gnuplotpp.hpp>
using namespace lc;

int main(int argc, char** argv)
{
    // Create a gnuplot instance
    Gnuplotpp gp;

    // generate some random numbers
    std::default_random_engine engine;
    std::normal_distribution generator;
    std::vector<double> data;
    for (size_t i = 0; i < 100; i++)
        data.push_back(generator(engine));

    // create a plot
    auto myPlot = gp.plot(data);
    
    // draw the plot
    gp.draw({ myPlot });

    return 0;
}

This generates the following plot:

Generating multiple plots

You can render multiple plots in this way:

auto myPlot1 = gp.plot(data1);
auto myPlot2 = gp.plot(data2);
gp.render({ myPlot1, myPlot2 });

Changing the plot style

You can change the plot styles in this way:

// Marker type and size
Gnuplotpp::Marker marker;
marker.pointType = Gnuplotpp::PointType::Circle;
marker.pointSize = 2;

// Setting a line with red color
Gnuplotpp::LineStyle lineStyle;
lineStyle.lineColor = "red";

// create a plot
auto myPlot = gp.plot(
    // plot data
    data,
    // we set the plot options using named aggregate initializer
    {
        .lineStyle = lineStyle,
        .marker = marker
    }
);
gp.draw({ myPlot });

Example of generated output:

unset style line 50
set style line 50 lc "red" pt 6 ps 2
plot '-'  using 1 with linespoint ls 50
-0.146382
0.13453
-1.87138
0.46065
-0.214253
EOD

Analyzing the output

You can see the generated gnuplot commands by substituting:

Gnuplotpp gp;

with

Gnuplotpp gp(std::ofstream("myfile.p"));

This would create a file named myfile.p with the generated gnuplot commands, for example:

plot '-' using 1 pt 1, '-' using 1 pt 1
-0.146382
0.13453
-1.87138
EOD

2.46065
1.78575
2.16371
EOD

You can run the generated output typing on the command line:

gnuplot --persist "myfile.p"