cpp 代码中的 gnuplot - 无法显示图形

Gnuplot in cpp code - Graphics can not be show

本文关键字:显示 显示图 图形 代码 gnuplot cpp      更新时间:2023-10-16

我在cpp代码中使用了gnuplot。我从指南中获取代码。我调整了这段代码没有错误,但图形无法显示。

法典:

#define GNUPLOT "gnuplot -persist"
int main(int argc, char **argv)
{
FILE *gp;
gp = popen(GNUPLOT,"w"); /* ’gp’ is the pipe descriptor */
if (gp==NULL)
{
    printf("Error opening pipe to GNU plot. Check if you have it! n");
    return 0;
}
fprintf(gp, "set samples 2000n");
fprintf(gp, "plot cos(x) n");
fprintf(gp, "rep sin(x) n");
fclose(gp);
return 0;
}

编辑 1

我尝试了另一个指南中的示例。我编译并运行它们没有错误,但图表没有出现。我使用Linux操作系统。

代码 2 :

#include <stdio.h>
 #include <plot.h>
 #include <math.h>
 #define SIZE 100.0   /* nominal size of user coordinate frame */
 #define EXPAND 2.2   /* expansion factor for elliptical box */
 void draw_boxed_string (plPlotter *plotter,
                         char *s, double size, double angle)
 {
   double true_size, width;
   pl_ftextangle_r (plotter, angle);      /* set text angle (degrees) */
   true_size = pl_ffontsize_r (plotter, size);  /* set font size */
   width = pl_flabelwidth_r (plotter, s); /* compute width of string */
   pl_fellipserel_r (plotter, 0.0, 0.0,
                     EXPAND * 0.5 * width, EXPAND * 0.5 * true_size,
                     angle);              /* draw surrounding ellipse */
   pl_alabel_r (plotter, 'c', 'c', s);    /* draw centered text string */
 }
 int main()
 {
   plPlotter *plotter;
   plPlotterParams *plotter_params;
   int i;
   /* set a Plotter parameter */
   plotter_params = pl_newplparams ();
   pl_setplparam (plotter_params, "PAGESIZE", "letter");
   /* create a Postscript Plotter that writes to standard output */
   if ((plotter = pl_newpl_r ("ps", stdin, stdout, stderr,
                              plotter_params)) == NULL)
     {
       fprintf (stderr, "Couldn't create Plottern");
       return 1;
     }
   if (pl_openpl_r (plotter) < 0)      /* open Plotter */
     {
       fprintf (stderr, "Couldn't open Plottern");
       return 1;
     }
   /* specify user coor system */
   pl_fspace_r (plotter, -(SIZE), -(SIZE), SIZE, SIZE);
   pl_pencolorname_r (plotter, "blue");     /* use blue pen */
   pl_fillcolorname_r (plotter, "white");   /* set white fill color */
   pl_filltype_r (plotter, 1);   /* fill ellipses with fill color */
   /* choose a Postscript font */
   pl_fontname_r (plotter, "NewCenturySchlbk-Roman");
   for (i = 80; i > 1; i--)      /* loop through angles */
     {
       double theta, radius;
       theta = 0.5 * (double)i;  /* theta is in radians */
       radius = SIZE / pow (theta, 0.35);  /* this yields a spiral */
       pl_fmove_r (plotter, radius * cos (theta), radius * sin (theta));
       draw_boxed_string (plotter, "GNU libplot!", 0.04 * radius,
                          (180.0 * theta / M_PI) - 90.0);
     }
   if (pl_closepl_r (plotter) < 0)        /* close Plotter */
     {
       fprintf (stderr, "Couldn't close Plottern");
       return 1;
     }
   if (pl_deletepl_r (plotter) < 0)       /* delete Plotter */
     {
       fprintf (stderr, "Couldn't delete Plottern");
       return 1;
     }
   return 0;
 }

你不仅应该运行./plot > a.ps ./plot