我使用 gnuplot 创建了一个每秒都会变化的图表。现在,我想在程序完成时将此图表保存为 gif 或 png 文件。我该怎么做?我的 C 代码如下
FILE *pipe = popen("gnuplot -persist", "w");
// set axis ranges
fprintf(pipe,"set xrange [0:11]\n");
fprintf(pipe,"set yrange [0:]\n");
int b = 5;int a;
// to make 10 points
std::vector<int> x (10, 0.0); // x values
std::vector<int> y (10, 0.0); // y values
for (a=0;a<5;a++) // 10 plots
{
x[a] = a;
y[a] = 2*a;// some function of a
fprintf(pipe,"plot '-'\n");
// 1 additional data point per plot
for (int ii = 0; ii <= a; ii++) {
fprintf(pipe, "%d %d\n", x[ii], y[ii]); // plot `a` points
}
fprintf(pipe,"e\n"); // finally, e
fflush(pipe); // flush the pipe to update the plot
usleep(1000000);// wait a second before updating again
}
答案1
我不会输入 c(看起来真的是 C++)部分 --- 您只需向发送正确的命令即可gnuplot
。
在 gnuplot 中,改变输出格式称为改变终端。
例如,要生成 PNG 文件,请在gnuplot
:
[...instruction to make youtr graph...]
set term pngcairo
set output "filename.png"
replot
set output
...将在名为 的文件中生成图形filename.png
。记得切换回你的终端(或使用 的另一个实例gnuplot
),并使用类似以下样式的内容
set term wxt persist
然后再开始密谋。
您有很多信息help set term pngcairo
:
gnuplot> help set term pngcairo The `pngcairo` terminal device generates output in png. The actual drawing is done via cairo, a 2D graphics library, and pango, a library for laying out and rendering text. Syntax: set term pngcairo {{no}enhanced} {mono|color} {solid|dashed} {{no}transparent} {{no}crop} {background <rgbcolor> {font <font>} {fontscale <scale>} {linewidth <lw>} {rounded|butt} {dashlength <dl>} {size <XX>{unit},<YY>{unit}} This terminal supports an enhanced text mode, which allows font and other formatting commands (subscripts, superscripts, etc.) to be embedded in labels and other text strings. The enhanced text mode syntax is shared with other gnuplot terminal types. See `enhanced` for more details.
有很多种格式的位图生成终端:看看
help set term jpeg
help set term gif
在该gif
模式下,您甚至可以生成动画 gif。请参阅:
gnuplot> set term gif animate
Terminal type set to 'gif'
Options are 'nocrop font "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf,12" fontscale 1.0 animate delay 10 loop 0 nooptimize size 640,480 '
gnuplot> set output "test.gif"
gnuplot> plot sin(x)
gnuplot> plot sin(x-1)
gnuplot> plot sin(x-2)
gnuplot> plot sin(x-3)
gnuplot> plot sin(x-4)
gnuplot> plot sin(x-5)
gnuplot> set output
End of animation sequence
...并且您有test.gif
: