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