c++gnuplotgnuplot-iostream

Provide Separate Colormap Values to 3D Plot in GNUPlot C++? (C++ 14, VS 22)


I have a 3D grid plot consisting of 3D points (0, 0, 0), (1, 1, 1) and (2, 2, 2). I'm graphing it using the GNUPlot implementation in C++ 14, and I'm using Visual Studio 2022.

The following code:

#include <iostream>
#include <string>
#include <vector>
#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 xlabel 'X-Axis'\n";
    gp << "set ylabel 'Y-Axis'\n";
    gp << "set zlabel 'Z-Axis'\n";

    gp << "set dgrid3d 20,20\n";

    gp << "splot" << gp.file1d(data) << "with pm3d title ' '" << endl;

    return 0;
}

Produces the following plot:

enter image description here

In the plot above, the colormap is mapped to the Z-axis.

While I'm able to change the colors of the colormap, I was wondering whether it would be possible to change what values the colormap is mapped to. I would want them to be mapped to a separate set of values (a 4-th dimension one might say), instead of the Z-axis values. Is that possible?

Thanks for reading my post, any guidance is appreciated.


Solution

  • Certainly. If you give a 4th column of input data, that will be used for the color mapping rather than the z coordinate. See for example this plot from on-line gnuplot demo collection: heatmap demos

    enter image description here