如何在C中传递gnuplot矩阵并绘制pm3d映射? [英] How to pass gnuplot a matrix and splot pm3d map in c?
问题描述
因此,这一次,我有一个名为zvalue[10][10]
的矩阵,我想绘制一个具有常规x-y
网格并且以zvalue[i][j]
为点(i,j)
的值的pm3d
贴图.
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)
.
在gnuplot中,我只使用:
In gnuplot, I just use:
set pm3d map
splot zvalue matrix using 1:2:3 with pm3d
在C++
gnuplot管道中,我可以使用函数gp.file1d()
来实现这一目标.
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"
但是现在,我在C gnuplot管道中,当然我可以将矩阵写入名为zvalue.txt
的文件中,并使用以下命令:
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")
但是还有其他方法吗?当我使用矩阵处理普通散点图时,我尝试了@Christ的建议,即执行以下操作:
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;
}
但是当它是pm3d
时,它不能很好地工作.我还逐点尝试了splot
,这对于带有矩阵的标称散点图非常有效,但是在这里,对于pm3d
,则没有任何效果.
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.
推荐答案
以下脚本在pm3d
上可以正常使用:
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;
}
因此,如果您有一个使用pm3d
且无法正常工作的程序,那么确实可以准确地看到 完整文件(以便您可以将代码复制并粘贴到进行编译和测试).
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).
这篇关于如何在C中传递gnuplot矩阵并绘制pm3d映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!