Passing data from C ++ to the gnuplot example (using the Gnuplot-iostream interface)

I just went through the Dan Stahlke gnuplot C ++ I / O interface, which saves me from "transfusing my own." Unfortunately, there are not so many examples, and there is no real documentation.

I have the following data types in my C ++ project:

struct Data
{
  std::string datestr;  // x axis value
  float f1;             // y axis series 1
  float f2;             // y axis series 2
  float f3;             // y axis series 3
};


typedef std::vector<Data> Dataset;

I want to pass a Dataset variable from C ++ so that I can build data (dates on the X axis and 3 numbers built as time series on the Y axis).

Can someone show me how to pass a Dataset variable from C ++ to gnuplot (using the Gnuplot-iostream interface) and make a simple graph using the transferred data?

+3
source share
3 answers

git, , . struct Data, TextSender. , .

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

struct Data {
    std::string datestr;  // x axis value
    float f1;             // y axis series 1
    float f2;             // y axis series 2
    float f3;             // y axis series 3
};

typedef std::vector<Data> Dataset;

namespace gnuplotio {
    template<>
    struct TextSender<Data> {
        static void send(std::ostream &stream, const Data &v) {
            TextSender<std::string>::send(stream, v.datestr);
            stream << " ";
            TextSender<float>::send(stream, v.f1);
            stream << " ";
            TextSender<float>::send(stream, v.f2);
            stream << " ";
            TextSender<float>::send(stream, v.f3);

            // This works too, but the longer version above gives
            // gnuplot-iostream a chance to format the numbers itself (such as
            // using a platform-independent 'nan' string).
            //stream << v.datestr << " " << v.f1 << " " << v.f2 << " " << v.f3;
        }
    };
}

int main() {
    Dataset x(2);
    // The http://www.gnuplot.info/demo/timedat.html example uses a tab between
    // date and time, but this doesn't seem to work (gnuplot interprets it as
    // two columns).  So I use a comma.
    x[0].datestr = "01/02/2003,12:34";
    x[0].f1 = 1;
    x[0].f2 = 2;
    x[0].f3 = 3;
    x[1].datestr = "02/04/2003,07:11";
    x[1].f1 = 10;
    x[1].f2 = 20;
    x[1].f3 = 30;

    Gnuplot gp;
    gp << "set timefmt \"%d/%m/%y,%H:%M\"\n";
    gp << "set xdata time\n";
    gp << "plot '-' using 1:2 with lines\n";
    gp.send1d(x);

    return 0;
}

. . example-data-1d.cc git .

, , operator<<(std::ostream &, ...).

- std::tuple ( ++ 11) boost::tuple . (, , , ).

+5

, gnuplot-iostream?

, , :

Gnuplot gp;

gp << "set terminal png\n";

std::vector<double> y_pts;
for(int i=0; i<1000; i++) {
    double y = (i/500.0-1) * (i/500.0-1);
    y_pts.push_back(y);
}

gp << "set output 'my_graph_1.png'\n";
gp << "plot '-' with lines, sin(x/200) with lines\n";
gp.send(y_pts);
+1

google.com

gnuplot ++

0
source

Source: https://habr.com/ru/post/1785729/


All Articles