Actual source code: ex9.c
petsc-3.6.1 2015-08-06
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 = 300,height = 300,bins = 8;
16: PetscInt w = 300,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,(char*)0,help);
25: PetscOptionsGetInt(NULL,"-width",&w,NULL);
26: PetscOptionsGetInt(NULL,"-height",&h,NULL);
27: PetscOptionsGetInt(NULL,"-n",&nn,NULL);
28: PetscOptionsGetInt(NULL,"-bins",&b,NULL);
29: PetscOptionsGetInt(NULL,"-color",&c,NULL);
30: PetscOptionsHasName(NULL,"-nolabels",&flg);
31: width = (int) w; height = (int)h; n = (int)nn; bins = (int) b; color = (int) c;
32: if (flg) {
33: xlabel = (char*)0; toplabel = (char*)0;
34: }
35: PetscDrawCreate(PETSC_COMM_SELF,0,"Title",x,y,width,height,&draw);
36: PetscDrawSetFromOptions(draw);
37: PetscDrawHGCreate(draw,bins,&hist);
38: PetscDrawHGGetAxis(hist,&axis);
39: PetscDrawAxisSetColors(axis,PETSC_DRAW_BLACK,PETSC_DRAW_RED,PETSC_DRAW_BLUE);
40: PetscDrawAxisSetLabels(axis,toplabel,xlabel,ylabel);
42: for (i=0; i<n; i++) {
43: xd = (PetscReal)(i - 5);
44: PetscDrawHGAddValue(hist,xd*xd);
45: }
46: PetscDrawHGSetColor(hist,color);
47: PetscDrawHGDraw(hist);
48: PetscDrawFlush(draw);
50: PetscDrawHGDestroy(&hist);
51: PetscDrawDestroy(&draw);
52: PetscFinalize();
53: return 0;
54: }