Actual source code: ex9.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  2: static char help[] = "Makes a simple histogram.\n";

  4: #include <petscsys.h>
  5: #include <petscdraw.h>

  9: int main(int argc,char **argv)
 10: {
 11:   PetscDraw      draw;
 12:   PetscDrawHG    hist;
 13:   PetscDrawAxis  axis;
 15:   int            n     = 20,i,x = 0,y = 0,width = 400,height = 300,bins = 8;
 16:   PetscInt       w     = 400,h = 300,nn = 20,b = 8,c = PETSC_DRAW_GREEN;
 17:   int            color = PETSC_DRAW_GREEN;
 18:   const char     *xlabel,*ylabel,*toplabel;
 19:   PetscReal      xd;
 20:   PetscBool      flg;

 22:   xlabel = "X-axis Label"; toplabel = "Top Label"; ylabel = "Y-axis Label";

 24:   PetscInitialize(&argc,&argv,NULL,help);
 25:   PetscOptionsGetInt(NULL,NULL,"-width",&w,NULL);
 26:   PetscOptionsGetInt(NULL,NULL,"-height",&h,NULL);
 27:   PetscOptionsGetInt(NULL,NULL,"-n",&nn,NULL);
 28:   PetscOptionsGetInt(NULL,NULL,"-bins",&b,NULL);
 29:   PetscOptionsGetInt(NULL,NULL,"-color",&c,NULL);
 30:   PetscOptionsHasName(NULL,NULL,"-nolabels",&flg);
 31:   width = (int) w; height = (int)h; n = (int)nn; bins = (int) b; color = (int) c;
 32:   if (flg) { xlabel = NULL; ylabel = NULL; toplabel = NULL; }

 34:   PetscDrawCreate(PETSC_COMM_WORLD,0,"Title",x,y,width,height,&draw);
 35:   PetscDrawSetFromOptions(draw);
 36:   PetscDrawHGCreate(draw,bins,&hist);
 37:   PetscDrawHGSetColor(hist,color);
 38:   PetscDrawHGGetAxis(hist,&axis);
 39:   PetscDrawAxisSetColors(axis,PETSC_DRAW_BLACK,PETSC_DRAW_RED,PETSC_DRAW_BLUE);
 40:   PetscDrawAxisSetLabels(axis,toplabel,xlabel,ylabel);
 41:   /*PetscDrawHGSetFromOptions(hist);*/

 43:   for (i=0; i<n; i++) {
 44:     xd   = (PetscReal)(i - 5);
 45:     PetscDrawHGAddValue(hist,xd*xd);
 46:   }
 47:   PetscDrawHGDraw(hist);
 48:   PetscDrawHGSave(hist);

 50:   PetscDrawHGDestroy(&hist);
 51:   PetscDrawDestroy(&draw);
 52:   PetscFinalize();
 53:   return 0;
 54: }