Plotting heatmap with Gnuplot in C++

不想你离开。 提交于 2020-01-02 21:53:52

问题


#include <vector>
#include <cmath>
#include <boost/tuple/tuple.hpp>
#include "gnuplot-iostream.h"

int main()
{
    float frame[4][4];
    for (int n=0; n<4; n++)
    {
        for (int m=0; m<4; m++)
        {
        frame[n][m]=n+m;
        }
    }
    Gnuplot gp;
    gp << "unset key\n";
    gp << "set pm3d\n";
    gp << "set hidden3d\n";
    gp << "set view map\n";
    gp << "set xrange [ -0.500000 : 3.50000 ] \n";
    gp << "set yrange [ -0.500000 : 3.50000 ] \n";
    gp << "splot '-'\n";
    gp.send2d(frame);
    gp.flush();
}

This generates me an image:

The problem is, that element "frame[0][0]" should be black in this color palette. And I suppose it is black, but the area between four points is colored in blue (color for average value of these four points). What I want, that I could generate an image like this: link to gnuplot heatmap example. It actually colors a matrix element, not the area between four elements. So how do I do that using C++ and an iostream pipe? I can print data to a file and then plot from that file, but this can be slow... Any ideas?


回答1:


You must use the image plotting style, just like done in the demo page you linked:

#include <vector>
#include <cmath>
#include <boost/tuple/tuple.hpp>
#include "gnuplot-iostream.h"

int main()
{
    float frame[4][4];
    for (int n=0; n<4; n++)
    {
        for (int m=0; m<4; m++)
        {
        frame[n][m]=n+m;
        }
    }
    Gnuplot gp;
    gp << "unset key\n";
    gp << "set autoscale fix\n";
    gp << "plot '-' with image\n";
    gp.send2d(frame);
    gp.flush();
}

I cannot test it right now, but that should do the trick. Maybe you need to change the set autoscale part to fit your needs. Using set autoscale fix uses tight ranges for all axes (x, y, and color).



来源:https://stackoverflow.com/questions/26777605/plotting-heatmap-with-gnuplot-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!