4

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

2 Answers 2

4

You want set terminal png instead.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your reply. i've made that change but i still can't open the png file :(
ok, i figured it out. i had to add set terminal png before the set output command
-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.