0
votes

So, this time, I have a matrix called zvalue[10][10], and I would like to draw a pm3d map, with normal x-y grids, and with zvalue[i][j] to be the value of point (i,j).

In gnuplot, I just use:

set pm3d map
splot zvalue matrix using 1:2:3 with pm3d

And in C++ gnuplot pipes, I can use a function gp.file1d() to achieve this.

gp << "splot" << gp.file1d(matrix) << "matrix using 2:1:3" << "with pm3d\n"

But now, I am in C gnuplot pipe, of course I can write the matrix into a file called zvalue.txt, and use the following:

fprintf(gp, "splot \"zvalue.txt\" matrix using 1:2:3 with pm3d\n")

But is there other way? I tried @Christ's suggestion when I deal with normal splot with matrix, that is do something like:

int main(void)
{
    FILE *gp = popen(GNUPLOT, "w");

    fprintf(gp, "splot '-' matrix using 1:2:3\n");
    int i, j;
    for (i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++)
            fprintf(gp, "%d ", i*j);
        fprintf(gp, "\n");
    }
    pclose(gp);
    return 0;
}

But when it's pm3d, it does not work well. I also tried with splot point by point, which works well for nomal splot with matrix, but here with pm3d, nothing works.

1
Please take some time and read stackoverflow.com/help/asking to get started with how stackoverflow works. Especially how your not-working (but full) example with pm3d looks like. And please also have a look at stackoverflow.com/editing-help to see how to properly format a question. That makes it much easier for others to read your question and to help. - Christoph
Thanks for reminding, I will take a look. - Robin

1 Answers

0
votes

The following script works fine with pm3d:

#include <stdio.h>
#define GNUPLOT "gnuplot -persist"

int main(void)
{
    FILE *gp = popen(GNUPLOT, "w");

    fprintf(gp, "set pm3d map\n");
    fprintf(gp, "splot '-' matrix using 1:2:3 with pm3d\n");
    int i, j;
    for (i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++)
            fprintf(gp, "%d ", i*j);
        fprintf(gp, "\n");
    }
    pclose(gp);
    return 0;
}

enter image description here

So if you have a program which makes use of pm3d and doesn't work, it would really help to see exactly that full file (so that one can copy&paste the code to compile and test it).