-1

I have a set of x and y values that I fit onto a polynomial using the gsl_multifit_linear function. I would like to plot the best-fit curve onto the scatterplot using gnuplot. Right now, I only figured out how to plot the points themselves:

FILE *gnuplot = popen("gnuplot", "w");
fprintf(gnuplot, "plot '-'\n");
for (int i = 0; i < num_points; i++)
   fprintf(gnuplot, "%g %g\n", xvals[i], yvals[i]);
printf(gnuplot, "e\n");
fflush(gnuplot);
  • 1
    this doesn't look like programing question. You are looking how for help how to use gnuplot. – Marek R Mar 23 '21 at 15:45
  • Write down a gnuplot script that does what you want. Then modify your code to mimic the script. Anyway, without knowing the parameters of the fit polynomial, is degree etc., it's impossible to give you a more specific answer/advice. – zkoza Mar 30 '21 at 01:38

1 Answers1

-1

Plot the function first, then the data:

fprintf(gnuplot, "plot x*x, '-'\n");

Here I assumed the function is just x*x. Alternatively, save the points to a temporary file and then plot it in a usual, gnuplot way in any order relative to the data.

zkoza
  • 2,644
  • 3
  • 16
  • 24