在 gnuplot 和 C 中将绘图保存为 png
我已经能够在运行程序时使用 gnuplot 来绘制图形,但现在我想将图形写入文件。以下代码打开一个绘图并创建一个 png,但该 png 无法打开(Gimp 说它已损坏)。诚然,我并不真正理解我编写的代码,因为它是从网上的片段中获取的。有谁知道出了什么问题吗?我想要做的就是能够将我的散点图保存为 png 格式。
#include <iostream>
#include "gnuplot_i.h"
#include <math.h>
using namespace std;
int main() {
double average_distance[5] = {1, 3, 5, 2, 4};
double x_coord[5] = {1, 2, 3, 4, 5};
gnuplot_ctrl* h1 = gnuplot_init();
gnuplot_setstyle(h1, "points");
gnuplot_cmd(h1, "set output 'test-plot-1.png'");
gnuplot_plot_xy(h1, x_coord, average_distance, 5, "plot");
gnuplot_cmd(h1,"set terminal x11" );
sleep(400);
return 0;
}
I have been able to use gnuplot to plot graphs as I run my program, but now I want to write the graphs to files. The following code opens a plot and creates a png, but the png does not open (Gimp says it is corrupted). Admittedly I don't really understand the code I've written, because it's taken from snippets online. Does anyone know what's wrong? All I want to do is be able to save my scatter graph as a png.
#include <iostream>
#include "gnuplot_i.h"
#include <math.h>
using namespace std;
int main() {
double average_distance[5] = {1, 3, 5, 2, 4};
double x_coord[5] = {1, 2, 3, 4, 5};
gnuplot_ctrl* h1 = gnuplot_init();
gnuplot_setstyle(h1, "points");
gnuplot_cmd(h1, "set output 'test-plot-1.png'");
gnuplot_plot_xy(h1, x_coord, average_distance, 5, "plot");
gnuplot_cmd(h1,"set terminal x11" );
sleep(400);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要
设置终端 png
。You want
set terminal png
instead.为什么不使用 MathGL (GPL 绘图库),它可以本机导出到 PNG/EPS/SVG/...甚至可以在控制台中做到这一点(即没有X)?
Why not to use MathGL (GPL plotting library) which can native export to PNG/EPS/SVG/... and can do it even in console (i.e. without X) ?