C++ cpp代码中的Gnuplot-无法显示图形

C++ cpp代码中的Gnuplot-无法显示图形,c++,linux,gnuplot,C++,Linux,Gnuplot,我在cpp代码中使用了gnuplot。我从指南中获取代码。我tun这个代码没有错误,但图形无法显示 代码: #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

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

代码:

#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 2000\n");
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 Plotter\n");
       return 1;
     }

   if (pl_openpl_r (plotter) < 0)      /* open Plotter */
     {
       fprintf (stderr, "Couldn't open Plotter\n");
       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 Plotter\n");
       return 1;
     }
   if (pl_deletepl_r (plotter) < 0)       /* delete Plotter */
     {
       fprintf (stderr, "Couldn't delete Plotter\n");
       return 1;
     }
   return 0;
 }
#包括
#包括
#包括
#定义尺寸100.0/*用户坐标系的标称尺寸*/
#定义椭圆盒的扩展2.2/*扩展因子*/
无效绘制装箱字符串(plplplotter*绘图仪,
字符*s,双尺寸,双角度)
{
双真实尺寸、宽度;
pl_ftextangle_r(绘图仪,角度);/*设置文字角度(度)*/
true_size=pl_ffontsize_r(绘图仪,大小);/*设置字体大小*/
宽度=pl_flabelwidth_r(绘图仪,s);/*计算字符串的宽度*/
pl_Fellipseell_r(绘图仪,0.0,0.0,
展开*0.5*宽度,展开*0.5*真实尺寸,
角度);/*绘制周围的椭圆*/
pl_alabel_r(绘图仪'c','c',s);/*绘制居中文本字符串*/
}
int main()
{
plPlotter*绘图仪;
plPlotterParams*绘图仪参数;
int i;
/*设置绘图仪参数*/
绘图仪参数=pl_newplparams();
plu setplparam(绘图仪参数,“页面大小”、“字母”);
/*创建写入标准输出的Postscript绘图仪*/
如果((绘图仪=pl_newpl_r(“ps”)、标准输入、标准输出、标准输出、,
绘图仪(参数))==NULL)
{
fprintf(stderr,“无法创建绘图仪”\n);
返回1;
}
如果(pl_openpl_r(绘图仪)<0)/*打开绘图仪*/
{
fprintf(stderr,“无法打开绘图仪”\n);
返回1;
}
/*指定用户或系统*/
pl_fspace_r(绘图仪,-(尺寸),-(尺寸),尺寸,尺寸);
pl_pencolorname_r(绘图仪,“蓝色”);/*使用蓝色笔*/
pl_fillcolorname_r(绘图仪,“白色”);/*设置白色填充颜色*/
pl_filltype_r(绘图仪,1);/*使用填充颜色填充椭圆*/
/*选择Postscript字体*/
pl_fontname_r(绘图仪,“新世纪罗马”);
对于(i=80;i>1;i--)/*循环通过角度*/
{
双θ,半径;
θ=0.5*(双)i;/*θ以弧度为单位*/
半径=大小/功率(θ,0.35);/*这产生一个螺旋*/
pl_fmove_r(绘图仪,半径*cos(θ),半径*sin(θ));
绘制装箱字符串(绘图仪,“GNU LIBPROOT!”,半径为0.04*,
(180.0*θ/M_π)-90.0);
}
如果(pl_closepl_r(绘图仪)<0)/*关闭绘图仪*/
{
fprintf(stderr,“无法关闭绘图仪”\n);
返回1;
}
如果(pl_deletepl_r(绘图仪)<0)/*删除绘图仪*/
{
fprintf(stderr,“无法删除绘图仪”\n);
返回1;
}
返回0;
}

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

你使用了哪本指南?由于数字地址的原因,我无法在此网站上添加指南的链接。你是说代码编译成功并运行了,但图形没有出现?是的,它符合并运行无误。它对我有效。我不得不将
#include
添加到您的代码顶部,但是,这样做效果很好。你在用什么操作系统?如果打开一个shell并直接在gnuplot中键入命令,会发生什么?