Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw set of points , lines in Gnuplot

Tags:

c++

gnuplot

I think this might be a really easy question for someone that has already used gnuplot and "splot" command, but I can't figure it right now as this is my first day using this program.

I achived linking my c++ project to gnuplot, so I'm able to create a Datafile.dat with this format:

# Xcoord Ycoord Zcoord
1 2 1
2 1 1
3 1 1
3 2 2
3 3 3

And in my C++ file, I do:

#include "gnuplot.h"
#include <iostream>
using namespace std;
int main() {
    Gnuplot plot;
    plot("set border 4095");
    plot("splot \"C:/\\Users/\\lRaulMN/\\Desktop/\\Datafile.dat\" with lines");
    return 0;
}

This works perfectly and I get this:

View

Now the question is: Given the 5 points I'm using, is there any way to splot those numbers without having to create the Datafile.dat?

Because in the future, I'll have something like this in my code:

#include "gnuplot.h"
#include <iostream>
#include <vector>
using namespace std;
typedef struct {
    double Time;
    double X;
    double Y;
    double Z;
} DimensionalPoint;

int main() {
    Gnuplot plot;
    plot("set border 4095"); 
    vector<DimensionalPoint> test;
    plot("splot \"C:/\\Users/\\lRaulMN/\\Desktop/\\Datafile.dat\" with lines");
    return 0;
}

So my idea was to fill the test vector with numbers (Calculate them in my c++ code) and then call splot somehow and represent those numbers.

The first idea that came to mind was to create a file with the numbers (in c++ project, while executing), then make "splot" to that file however I'd be creating a lot of files for each interaction (because I'll have several vectors) and I don't want to end up using this solution.

I guess there won't be a super easy way to insert a vector of 3D points into gnuplot, but I can deal with that as soon as I know how to "splot" at least two numbers with X,Y and Z coordinates.

like image 316
Rauññ Avatar asked Oct 16 '22 06:10

Rauññ


1 Answers

Thanks to @Thor and @Bob, I found the solution.

First I create a "doubletoString" method:

string doubletoString(double value) {
    std::ostringstream origin;
    origin << value;
    std::string str = origin.str();
    return str;
}

Then I create a few 3D points just to try.

vector<DimensionalPoint> test;
DimensionalPoint A, B, C, D;
A.X = 1; A.Y = 1; A.Z = 1;
B.X = 2; B.Y = 2; B.Z = 3;
C.X = 1.2; C.Y = 2.4; C.Z = 2.3;
D.X = 8; D.Y = 3; D.Z = 1;
test.push_back(A);
test.push_back(B);
test.push_back(C);
test.push_back(D);

And then, I send this messages to GNUplot.

plot("set border 4095");
plot("$DATA << EOD"); 
double Xaux, Yaux, Zaux;
for (int i = 0; i < test.size(); i++) {
    Xaux = test.at(i).X;
    Yaux = test.at(i).Y;
    Zaux = test.at(i).Z;        
    plot(doubletoString(Xaux) + " " + doubletoString(Yaux) + " " + 
    doubletoString(Zaux));
}
plot("EOD");
plot("splot $DATA with lines");

This is the plot result when I execute the code.

Plot generated by GNUplot

like image 118
Rauññ Avatar answered Nov 15 '22 08:11

Rauññ