gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:02:48 25 4
gpt4 key购买 nike

我在 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 2000\n");
fprintf(gp, "plot cos(x) \n");
fprintf(gp, "rep sin(x) \n");
fclose(gp);
return 0;
}

编辑 1

我尝试了 another guide 中的示例。我编译并运行它们没有错误,但图表没有出现。我使用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;
}

最佳答案

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

关于c++ - C++ 代码中的 Gnuplot - 图形无法显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27336132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com