Search code examples
cterminalgnuplot

multiple line-plots in gnuplot using C


For a terminal application written in C I need to plot (in the terminal) two tables as line plots. To do this I use (following e.g. gnu tutorial)

 FILE *gnuplot = popen("gnuplot", "w");
 ...
  fprintf(gnuplot, "plot '-' u 1:2 t 'data1' w lp lt 0\n");
      for (int i = 0; i < lines; ++i) {
        fprintf(gnuplot,"%f %f \n", x[i] - x[0], y[i]);
    }
fprintf(gnuplot, "\n");
  fprintf(gnuplot, "t 'data2' lt 2\n");
    for (int i = 0; i < lines; ++i) {
        fprintf(gnuplot,"%f %f \n", x[i] - x[0], 0.4*(x[i] - c0)/c1);
    }
    fprintf(gnuplot, "e\n");
    fflush(gnuplot);

The problem is that the data is plotted as one data block instead of two different ones. I expected to have a separation by using the line

fprintf(gnuplot, "t 'data2' lt 2\n");

which seems not to be the case. What is wrong in this code?


Solution

  • You probably want to do the following. Each inline data has to be terminated by an e. In gnuplot console it would be:

    plot '-' u 1:2 t 'data1' w lp lt 0, '' u 1:2 t 'data2' lt 2
     1  0.1
     2  0.2
     3  0.3
     4  0.4
    e
    11  0.5
    12  0.6
    13  0.7
    14  0.8
    e
    

    Check help inline data and help special-filenames. Although, to my opinion it is a bit confusing and suitable examples are missing. Maybe you can find them elsewhere.