Search code examples
c++gnuplot

gnuplot visual studio invalid command error in console


This is the following code I have for plotting a series of random points in gnuplot. I have no errors in gnuplot-iostream header.

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

#include "gnuplot-iostream.h"

int main() {
    Gnuplot gp("\"C:\\Program Files\\gnuplot\\bin\\gnuplot.exe\"");

    std::random_device rd;
    std::mt19937 mt(rd());
    std::normal_distribution<double> normdist(0., 1.);

    std::vector<double> v0, v1;
    for (int i = 0; i < 1000; i++) {
        v0.push_back(normdist(mt));
        v1.push_back(normdist(mt));
    }
    std::partial_sum(v0.begin(), v0.end(), v0.begin());
    std::partial_sum(v1.begin(), v1.end(), v1.begin());

    gp << "set title 'graph of two random lines'\n";
    gp << "plot '-' with lines title v0," 
        << "'-' with lins title 'v1'\n";
    gp.send(v0);
    gp.send(v1);

    std::cin.get();
}

In console I am getting the following output:

gnuplot> -7.293891473275246
         ^
         line 1001: invalid command


gnuplot> -6.2938345608263884
         ^
         line 1001: invalid command

Thank you very much for assisting. I am new to gnuplot and appreciate it!


Solution

  • For debugging I used 5 points instead of 1000, so it was easier to see the first error:

    line 6: undefined variable: v0
    

    It turns out that the title of the first plot must be quoted:

    gp << "plot '-' with lines title 'v0',"
    

    There is also a typo in the second one, it must be lines instead of lins:

        << "'-' with lines title 'v1'\n";