PGPLOT Examples using fortran and c language.

本文介绍了PGPLOT库如何轻松生成简单至复杂的图形,通过六个子例程调用实现了函数y=x²*exp(-x)的绘图,并展示了如何在不修改源代码的情况下生成不同格式的文件。此外,还提供了Fortran和C语言的代码示例,以及在C语言中使用cpgplotlib库的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PGPLOT Examples

PGPLOT is designed to make it easy to generate simple graphs, while providing full capability for more complex graphs. As an example of a simple graph, here is a graph of the function y = x� exp(-x), which was drawn with six PGPLOT subroutine calls. [graphics] This figure is a GIF file. With no change to the source code, the program can also generate files in other formats, such as PostScript. Here is the Fortran code required to draw this graph, with comments to explain the purpose of each subroutine call. The question mark as an argument to PGOPENallows the user to specify a file name and file format at run time.
      PROGRAM EX1
      INTEGER PGOPEN, I
      REAL XS(9), YS(9), XR(101), YR(101)

C Compute numbers to be plotted.

      DO 10 I=1,101
          XR(I) = 0.1*(I-1)
          YR(I) = XR(I)**2*EXP(-XR(I))
 10   CONTINUE
      DO 20 I=1,9
          XS(I) = I
          YS(I) = XS(I)**2*EXP(-XS(I))
 20   CONTINUE

C Open graphics device.

      IF (PGOPEN('?') .LT. 1) STOP

C Define coordinate range of graph (0 < x < 10, 0 < y < 0.65),
C and draw axes.

      CALL PGENV(0., 10., 0., 0.65,  0,  0)

C Label the axes (note use of \u and \d for raising exponent).

      CALL PGLAB('x', 'y', 'PGPLOT Graph: y = x\u2\dexp(-x)')

C Plot the line graph.

      CALL PGLINE(101, XR, YR)

C Plot symbols at selected points.

      CALL PGPT(9, XS, YS, 18)

C Close the graphics device.

      CALL PGCLOS

      END
The same program can be written in C, using the cpgplot library.
#include "cpgplot.h"
#include "math.h"

int main()
{
   int i;
   float xs[9], ys[9];
   float xr[101], yr[101];

   /* Compute numbers to be plotted. */

   for (i=0; i<101; i++) {
     xr[i] = 0.1*i;
     yr[i] = xr[i]*xr[i]*exp(-xr[i]);
   }
   for (i=0; i<9; i++) {
     xs[i] = i+1;
     ys[i] = xs[i]*xs[i]*exp(-xs[i]);
   }

   /* Open graphics device. */

   if (cpgopen("?") < 1)
     return 1;

   /* Define coordinate range of graph (0 < x < 10, 0 < y < 0.65),
      and draw axes. */

   cpgenv(0., 10., 0., 0.65, 0, 0);

   /* Label the axes (note use of \\u and \\d for raising exponent). */

   cpglab("x", "y", "PGPLOT Graph: y = x\\u2\\dexp(-x)");

   /*  Plot the line graph. */

   cpgline(101, xr, yr);

   /* Plot symbols at selected points. */

   cpgpt(9, xs, ys, 18);

   /* Close the graphics device */

   cpgclos();
   return 0;
}

转载于:https://www.cnblogs.com/shaoguangleo/archive/2011/11/21/2806063.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值