如何使用 C 和 gnuplot 在一张图中打印两个或多个图

How to print two or more plots in one graph using C and gnuplot

我对 C 有点陌生,我想使用 gnuplot 在 C 中绘制数据。现在我有这个代码:

FILE *gnuplot = popen("gnuplot", "w");
fprintf(gnuplot, "plot '-' lc 6 w lp\n");
for (int i = 0; i < size; i++){
    fprintf(gnuplot, "%g %g \n", x[i], y[i] );
}
fprintf(gnuplot, "e\n");
fflush(gnuplot);

它一次打印一个图并且工作得很好,但是我需要在同一坐标中同时绘制两个图。有没有办法在我的程序中包含一些代码行,或者我应该使用完全不同的方法?

您可以使用内联数据块(manual 中的第 46 页),例如:

#include <stdio.h>

int main(void) {
  int size = 10;
  FILE *gnuplot = popen("gnuplot -persist", "w");

  fprintf(gnuplot, "$MyData << EOD\n");

  for (int i = 0; i < size; i++){
    fprintf(gnuplot, "%d %d\n", i, size-i );
  }

  fprintf(gnuplot, "EOD\n");

  fprintf(gnuplot, "plot $MyData u 0:1 lc 6 w lp, $MyData u 0:2 w l\n");
  fflush(gnuplot);
  return 0;
}