Create a PNG from GNUPLOT using c++ gnuplot-iostream.h

Viewed 72

I am trying to learn to use GNUPLOT from C++ using gnuplot-iostream.h

I wrote a very simple program to create an image and save it as PNG but is not working

this is the code

#include <iostream>
#include <vector>
#include <random>
#include <numeric>
#include "gnuplot-iostream.h"

void demo_0()
{
    Gnuplot gt("\"C:\\Program Files\\gnuplot\\bin\\gnuplot.exe\"");
    std::vector<double> v(10,0);
    std::iota(v.begin(), v.end(), 0);
    std::transform(v.begin(), v.end(), v.begin(), [](auto x) {return x * sin(1.5 * x); });
    gt << "set terminal png\n";
    gt << "set output 'b.png'\n";
    gt << "plot '-' with lines\n";
    gt.send(v);
    std::cin.get();
    
}

int main()
{
    
    demo_0();
    int n = 1;

    return n;
}

the error is

gnuplot> t output 'b.png'
         ^
         line 0: invalid command

If I comment the set terminal and set outputs lines it works fine and generates a plot. But I need to learn how to save it from the program

Thanks in advance for any help.

NOTE: I edited to put set output after terminal and before plot as advised.

0 Answers
Related