使用c++源代码在gnuplot中设置参数

Set parameters in gnuplot using C++ source code

本文关键字:设置 参数 gnuplot c++ 源代码 使用      更新时间:2023-10-16

我试图用gnuplot从c++源代码绘制一些矢量。我已经读过一个关于如何将c++与gnuplot连接起来的问题。

这是源代码:

int main()
{
    char * commandsForGnuplot[] = {"set title "Trajectory_ARM"","plot 'data.temp' with linespoints"};
    double xvals[NUM_POINTS] = {1.0, 2.0, 3.0, 4.0, 5.0};
    double yvals[NUM_POINTS] = {5.0 ,3.0, 1.0, 3.0, 5.0};
    FILE * temp = fopen("data.temp", "w");
    /*Opens an interface that one can use to send commands as if they were typing into the
     *     gnuplot command line.  "The -persistent" keeps the plot open even after your
     *     C program terminates.
     */
    FILE * gnuplotPipe = popen ("gnuplot -persistent", "w");
    int i;
    for (i=0; i < NUM_POINTS; i++)
    {
        fprintf(temp, "%lf %lf n", xvals[i]  , yvals[i]); //Write the data to a temporary file
    }
    for (i=0; i < NUM_COMMANDS; i++)
    {
        fprintf(gnuplotPipe, "%s n", commandsForGnuplot[i]); //Send commands to gnuplot one by one.
    }
    return 0;
}

问题是:

我有一个新的5点向量(a)我想在同一个图中有a和关于yval的xval。我应该如何修改我的代码?在本例中,我想用两种不同的颜色绘制A和xval

这是用c格式化的完整源代码。我刚刚添加了第三个向量A,以及commandsForGnuplot ('' u 1:3 w lp)的第三个指令,该指令将此向量A绘制为相对于xvals。注意,gnuplot默认情况下用不同的颜色绘制每条线。

#include <stdio.h>
#include <stdlib.h>
#define NUM_POINTS   5    // each vector has 5 points
int main()
{
  double xvals[NUM_POINTS] = {1.0, 2.0, 3.0, 4.0, 5.0};
  double yvals[NUM_POINTS] = {5.0 ,3.0, 1.0, 3.0, 5.0};
  double Avals[NUM_POINTS] = {4.0 ,5.0, 3.0, 0.0, 1.0};
  int i;
  //Write xvals, yvals, and Avals to a temporary file              
  FILE * temp = fopen("data.temp", "w");
  for(i=0; i < NUM_POINTS; i++)
    fprintf(temp, "%lf %lf %lfn", xvals[i]  , yvals[i], Avals[i]);
  fclose(temp);   // close file if it won't be used anymore
  // open interface to gnuplot. Option -p lets plot windows survive after program exits
  FILE * gnuplotPipe = popen ("gnuplot -p", "w");
  // send commands to gnuplot one by one.
  fprintf(gnuplotPipe, "%s n", "set title 'Trajectory\_ARM'");      // command 0: define the graph title
  fprintf(gnuplotPipe, "%s n", "plot 'data.temp' u 1:2 w lp");       // command 1: plot yvals vs xvals, and Avals vs xvals
  fflush(gnuplotPipe);      // plots the data with the current commands
  // we obtained a plot of data.temp
  // now, we place an instruction to pause the program until a key is typed
  getchar();                
  // add a plot to the current graph
  fprintf(gnuplotPipe, "%s n", "replot 'data.temp' u 1:3 w lp");
  pclose(gnuplotPipe);      // close gnuplot if it won't be used anymore
  return 0;
}

我想你可以使用multiplot命令

set multiplot
plot data1.temp
plot data2.temp
unset multiplot