在C ++中使用Gnuplot绘制热图 [英] Plotting heatmap with Gnuplot in C++

查看:280
本文介绍了在C ++中使用Gnuplot绘制热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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:

问题是,在此调色板中,元素"frame [0] [0]"应为黑色.我想它是黑色的,但是四个点之间的区域是蓝色的(这四个点的平均值是彩色的). 我想要的是可以生成这样的图像: 链接到gnuplot热图示例. 它实际上是为矩阵元素着色,而不是为四个元素之间的区域着色. 那么,如何使用C ++和iostream管道来做到这一点呢? 我可以将数据打印到文件中,然后从该文件进行打印,但这可能会很慢... 有什么想法吗?

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?

推荐答案

您必须使用image绘图样式,就像在链接的演示页中所做的一样:

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();
}

我现在无法对其进行测试,但这应该可以解决问题.也许您需要更改set autoscale部分以适合您的需求.使用set autoscale fix对所有轴(x,y和颜色)使用紧密范围.

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).

这篇关于在C ++中使用Gnuplot绘制热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆