0

I'm attempting to plot a set of 3D points in C++ using the GNUPlot library implementation. I'm using C++ 14 with Visual Studio 2022.

I understand how to plot 2D points with this library, however I'm quite confused as to how I'm supposed to plot a set of 3D points.

Let's say my 3D points are (0, 0, 0), (1, 1, 1), and (2, 2, 2).

One of the things I tried was to create a vector for each 3D point.

I tried the following example code from the GNUPlot examples page:

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

using namespace std;

int main()
{
    vector<vector<float>> data = {};

    data.push_back(vector<float>{0, 0, 0});
    data.push_back(vector<float>{1, 1, 1});
    data.push_back(vector<float>{2, 2, 2});

    Gnuplot gp;

    gp << "set title 'test'\n";

    gp << "set dgrid3d 30,30\n";
    gp << "set hidden3d\n";

    gp << "splot" << gp.file1d(data) << "u 1:2:3 with lines title 'test'" << std::endl;

    return 0;
}

However that gives me the following plot: enter image description here

I also tried to create a dedicated struct for the 3D points, however that resulted in an error from the GNUPlot header file.

When I would graph 2D points, I would use the std::pair datatype for representing the points, so what datatype should I use to represent 3D points?

Thank you for reading my post, any guidance is appreciated.

Runsva
  • 365
  • 1
  • 7
  • 2
    What did you expect? You have only three points and tell GnuPlot to render a 30x30 grid. You obtain a 30x30 grid that covers your three points and interpolates the rest. As per the documentation: http://gnuplot.info/docs_5.5/loc10942.html "A grid with dimensions derived from a bounding box of the scattered data and size as specified by the row/col_size parameters is created for plotting and contouring. The grid is equally spaced in x (rows) and in y (columns); the z values are computed as weighted averages or spline interpolations of the scattered points' z values." – ypnos Feb 06 '23 at 23:16
  • Oh okay, thanks for clarifying. I had no idea that this created a grid automatically for me. However, how would I go about plotting just the points? Like a 3D scatter plot? – Runsva Feb 06 '23 at 23:37
  • I believe you can use splot without dgrid3d. But I never used it myself, so.. – ypnos Feb 07 '23 at 09:18

1 Answers1

0

It was much simpler than expected, simply removing dgrid3d and replacing lines with points appears to do the trick (thanks ypnos):

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

using namespace std;

int main()
{
    vector<vector<float>> data = {};

    data.push_back(vector<float>{0, 0, 0});
    data.push_back(vector<float>{1, 1, 1});
    data.push_back(vector<float>{2, 2, 2});

    Gnuplot gp;

    gp << "set title 'test'\n";

    gp << "splot" << gp.file1d(data) << "u 1:2:3 with points pt 5 title 'test'" << std::endl;

    return 0;
}

Output:

enter image description here

Runsva
  • 365
  • 1
  • 7