c++gnuplotgnuplot-iostream

Graphing 3D Plot with C++ GNUPlot? (C++ 14, VS 22)


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.


Solution

  • 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