9.3. Line Graphs

Up: Contents Next: Graphical Convergence Monitor Previous: Simple Drawing

PETSc includes a set of routines for manipulating simple two-dimensional graphs. These routines, which begin with DrawAxisDraw(), are usually not used directly by the application programmer. Instead, the programmer employs the line graph routines to draw simple line graphs. As shown in the program, within Figure 14 , line graphs are created with the command

   ierr = DrawLGCreate(Draw win,int ncurves,DrawLG *ctx); 
The argument ncurves indicates how many curves are to be drawn. Points can be added to each of the curves with the command
   ierr = DrawLGAddPoint(DrawLG ctx,double *x,double *y); 
The arguments x and y are arrays containing the next point value for each curve. Several points for each curve may be added with
   ierr = DrawLGAddPoints(DrawLG ctx,int n,double **x,double **y); 
The line graph is drawn (or redrawn) with the command
   ierr = DrawLGDraw(DrawLG ctx); 
A line graph that is no longer needed can be destroyed with the command
   ierr = DrawLGDestroy(DrawLG ctx); 
To plot new curves, one can reset a linegraph with the command
   ierr = DrawLGReset(DrawLG ctx); 
The line graph automatically determines the range of values to display on the two axes. The user can change these defaults with the command
   ierr = DrawLGSetLimits(DrawLG ctx,double xmin,double xmax,double ymin,double ymax); 
It is also possible to change the display of the axes and to label them. This procedure is done by first obtaining the axes context with the command
   ierr = DrawLGGetAxis(DrawLG ctx,DrawAxis *axis); 
One can set the axes' colors and labels, respectively, by using the commands
   ierr = DrawAxisSetColors(DrawAxis axis,int axis_lines,int ticks,int text); 
   ierr = DrawAxisSetLabels(DrawAxis axis,char *top,char *x,char *y); 

#ifdef PETSC_RCS_HEADER 
static char vcid[] = "$Id: ex3.c,v 1.30 1999/03/19 21:16:22 bsmith Exp $"; 
#endif 
 
static char help[] = "Plots a simple line graph\n"; 
 
#include "petsc.h" 
 
#undef __FUNC__ 
#define __FUNC__ "main" 
int main(int argc,char **argv) 
{ 
  Draw     draw; 
  DrawLG   lg; 
  DrawAxis axis; 
  int      n = 20, i, ierr, x = 0, y = 0, width = 300, height = 300,flg; 
  char     *xlabel, *ylabel, *toplabel; 
  double   xd, yd; 
 
  xlabel = "X-axis Label";toplabel = "Top Label";ylabel = "Y-axis Label"; 
 
  PetscInitialize(&argc,&argv,(char*)0,help); 
  OptionsGetInt(PETSC_NULL,"-width",&width,&flg);  
  OptionsGetInt(PETSC_NULL,"-height",&height,&flg); 
  OptionsGetInt(PETSC_NULL,"-n",&n,&flg); 
  OptionsHasName(PETSC_NULL,"-nolabels",&flg);  
  if (flg) { 
    xlabel = (char *)0; toplabel = (char *)0; 
  } 
  ierr = DrawOpenX(PETSC_COMM_SELF,0,"Title",x,y,width,height,&draw);CHKERRA(ierr); 
  ierr = DrawLGCreate(draw,1,&lg); CHKERRA(ierr); 
  ierr = DrawLGGetAxis(lg,&axis); CHKERRA(ierr); 
  ierr = DrawAxisSetColors(axis,DRAW_BLACK,DRAW_RED,DRAW_BLUE); CHKERRA(ierr); 
  ierr = DrawAxisSetLabels(axis,toplabel,xlabel,ylabel); CHKERRA(ierr); 
 
  for ( i=0; i<n ; i++ ) { 
    xd = (double)( i - 5 ); yd = xd*xd; 
    ierr = DrawLGAddPoint(lg,&xd,&yd); CHKERRA(ierr); 
  } 
  ierr = DrawLGIndicateDataPoints(lg); CHKERRA(ierr); 
  ierr = DrawLGDraw(lg); CHKERRA(ierr); 
  ierr = DrawFlush(draw); CHKERRA(ierr); PetscSleep(2); 
 
  ierr = DrawLGDestroy(lg); CHKERRA(ierr); 
  ierr = DrawDestroy(draw); CHKERRA(ierr); 
  PetscFinalize(); 
  return 0; 
} 
  

Figure 14: Example of Drawing Plots

It is possible to turn off all graphics with the option -nox. This will prevent any windows from being opened or any drawing actions to be done. This is useful for running large jobs when the graphics overhead is too large, or for timing.


Up: Contents Next: Graphical Convergence Monitor Previous: Simple Drawing