Actual source code: ex9.c
petsc-3.14.6 2021-03-30
2: static char help[] = "Makes a simple histogram.\n";
4: #include <petscsys.h>
5: #include <petscdraw.h>
7: int main(int argc,char **argv)
8: {
9: PetscDraw draw;
10: PetscDrawHG hist;
11: PetscDrawAxis axis;
13: int n = 20,i,x = 0,y = 0,width = 400,height = 300,bins = 8;
14: PetscInt w = 400,h = 300,nn = 20,b = 8,c = PETSC_DRAW_GREEN;
15: int color = PETSC_DRAW_GREEN;
16: const char *xlabel,*ylabel,*toplabel;
17: PetscReal xd;
18: PetscBool flg;
20: xlabel = "X-axis Label"; toplabel = "Top Label"; ylabel = "Y-axis Label";
22: PetscInitialize(&argc,&argv,NULL,help);if (ierr) return ierr;
23: PetscOptionsGetInt(NULL,NULL,"-width",&w,NULL);
24: PetscOptionsGetInt(NULL,NULL,"-height",&h,NULL);
25: PetscOptionsGetInt(NULL,NULL,"-n",&nn,NULL);
26: PetscOptionsGetInt(NULL,NULL,"-bins",&b,NULL);
27: PetscOptionsGetInt(NULL,NULL,"-color",&c,NULL);
28: PetscOptionsHasName(NULL,NULL,"-nolabels",&flg);
29: width = (int) w; height = (int)h; n = (int)nn; bins = (int) b; color = (int) c;
30: if (flg) { xlabel = NULL; ylabel = NULL; toplabel = NULL; }
32: PetscDrawCreate(PETSC_COMM_WORLD,0,"Title",x,y,width,height,&draw);
33: PetscDrawSetFromOptions(draw);
34: PetscDrawHGCreate(draw,bins,&hist);
35: PetscDrawHGSetColor(hist,color);
36: PetscDrawHGGetAxis(hist,&axis);
37: PetscDrawAxisSetColors(axis,PETSC_DRAW_BLACK,PETSC_DRAW_RED,PETSC_DRAW_BLUE);
38: PetscDrawAxisSetLabels(axis,toplabel,xlabel,ylabel);
39: /*PetscDrawHGSetFromOptions(hist);*/
41: for (i=0; i<n; i++) {
42: xd = (PetscReal)(i - 5);
43: PetscDrawHGAddValue(hist,xd*xd);
44: }
45: PetscDrawHGDraw(hist);
46: PetscDrawHGSave(hist);
48: PetscDrawHGDestroy(&hist);
49: PetscDrawDestroy(&draw);
50: PetscFinalize();
51: return ierr;
52: }
56: /*TEST
58: build:
59: requires: x
61: test:
62: output_file: output/ex1_1.out
64: TEST*/