source: trunk/src/Devel/plottingUtilities.cc @ 288

Last change on this file since 288 was 288, checked in by Matthew Whiting, 17 years ago

Made a src/Devel/ directory, and put files from src/Utils/ that aren't used by Duchamp in it.

File size: 3.5 KB
Line 
1#include <iostream>
2#include <math.h>
3#include <cpgplot.h>
4#include <Utils/utils.hh>
5
6void plotLine(const float slope, const float intercept)
7{
8  float x1, x2, y1, y2;
9  cpgqwin(&x1,&x2,&y1,&y2);
10  cpgmove(x1,slope*x1+intercept);
11  cpgdraw(x2,slope*x2+intercept);
12}
13
14void lineOfEquality()
15{
16  plotLine(1.,0.);
17}
18
19void lineOfBestFit(int size, float *x, float *y)
20{
21  float a,b,r,erra,errb;
22  linear_regression(size,x,y,0,size-1,a,erra,b,errb,r);
23  plotLine(a,b);
24}
25
26void plotVertLine(const float xval, const int colour, const int style)
27{
28  float x1, x2, y1, y2;
29  cpgqwin(&x1,&x2,&y1,&y2);
30  int currentColour,currentStyle;
31  cpgqci(&currentColour);
32  cpgqls(&currentStyle);
33  if(currentColour!=colour) cpgsci(colour);
34  if(currentStyle !=style)  cpgsls(style);
35  cpgmove(xval,y1);
36  cpgdraw(xval,y2);
37  if(currentColour!=colour) cpgsci(currentColour);
38  if(currentStyle !=style)  cpgsls(currentStyle);
39}
40
41void plotVertLine(const float xval)
42{
43  int colour,style;
44  cpgqci(&colour);
45  cpgqls(&style);
46  plotVertLine(xval,colour,style);
47}
48
49void plotVertLine(const float xval, const int colour)
50{
51  int style;
52  cpgqls(&style);
53  plotVertLine(xval,colour,style);
54}
55
56void plotHorizLine(const float yval, const int colour, const int style)
57{
58  float x1, x2, y1, y2;
59  cpgqwin(&x1,&x2,&y1,&y2);
60  int currentColour,currentStyle;
61  cpgqci(&currentColour);
62  cpgqls(&currentStyle);
63  if(currentColour!=colour) cpgsci(colour);
64  if(currentStyle !=style)  cpgsls(style);
65  cpgmove(x1,yval);
66  cpgdraw(x2,yval);
67  if(currentColour!=colour) cpgsci(currentColour);
68  if(currentStyle !=style)  cpgsls(currentStyle);
69}
70
71void plotHorizLine(const float yval)
72{
73  int colour,style;
74  cpgqci(&colour);
75  cpgqls(&style);
76  plotHorizLine(yval,colour,style);
77}
78
79void plotHorizLine(const float yval, const int colour)
80{
81  int style;
82  cpgqls(&style);
83  plotHorizLine(yval,colour,style);
84}
85
86void lineOfBestFitPB(const int size, const float *x, const float *y)
87{
88  int numSim = int(size * log(float(size)*log(float(size))) );
89  float slope=0, intercept=0;
90  float *xboot = new float[size];
91  float *yboot = new float[size];
92  for(int sim=0;sim<numSim;sim++){
93    for(int i=0;i<size;i++){
94      int point = int((1.*rand())/(RAND_MAX+1.0) * size);
95      xboot[i] = x[point];
96      yboot[i] = y[point];
97    }
98    float a,b,r,erra,errb;
99    linear_regression(size,xboot,yboot,0,size-1,a,erra,b,errb,r);
100    slope += a;
101    intercept += b;
102  }
103  delete [] xboot;
104  delete [] yboot;
105  slope /= float(numSim);
106  intercept /= float(numSim);
107 
108  plotLine(slope,intercept);
109}
110
111void drawContours(const int size, const float *x, const float *y)
112{
113  float x1, x2, y1, y2;
114  cpgqwin(&x1,&x2,&y1,&y2);
115  const int nbin = 30;
116  // widths of bins in x and y directions: have one bin either side of
117  // extrema
118  float binWidthX = (x2 - x1) / float(nbin-2);
119  float binWidthY = (y2 - y1) / float(nbin-2);
120  float *binnedarray = new float[nbin*nbin];
121  for(int i=0;i<nbin*nbin;i++) binnedarray[i] = 0.;
122  for(int i=0;i<size;i++){
123    int xbin = int( (x[i] - (x1-binWidthX)) / binWidthX );
124    int ybin = int( (y[i] - (y1-binWidthY)) / binWidthY );
125    int binNum = ybin*nbin + xbin;
126    if((binNum>=0)&&(binNum<nbin*nbin)) binnedarray[binNum] += 1.;
127  } 
128  float maxct = binnedarray[0];
129  for(int i=1;i<nbin*nbin;i++) if(binnedarray[i]>maxct) maxct=binnedarray[i];
130  float tr[6]={x1-3*binWidthX/2,binWidthX,0.,y1-3*binWidthY/2,0.,binWidthY};
131  const int ncont = 10;
132  float *cont = new float[ncont];
133  for(int i=0;i<ncont;i++) cont[i] = maxct / pow(2,float(i)/2.);
134  cpgcont(binnedarray,nbin,nbin,1,nbin,1,nbin,cont,ncont,tr);
135  delete [] binnedarray;
136  delete [] cont;
137}
Note: See TracBrowser for help on using the repository browser.