[301] | 1 | // ----------------------------------------------------------------------- |
---|
| 2 | // plottingUtilities.cc: Functions to simplify basic PGPLOT commands, |
---|
| 3 | // plus one to draw a contour map of a set |
---|
| 4 | // of points. |
---|
| 5 | // ----------------------------------------------------------------------- |
---|
| 6 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
| 7 | // |
---|
| 8 | // This program is free software; you can redistribute it and/or modify it |
---|
| 9 | // under the terms of the GNU General Public License as published by the |
---|
| 10 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
| 11 | // option) any later version. |
---|
| 12 | // |
---|
| 13 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
| 14 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 15 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
| 16 | // for more details. |
---|
| 17 | // |
---|
| 18 | // You should have received a copy of the GNU General Public License |
---|
| 19 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
| 20 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
| 21 | // |
---|
| 22 | // Correspondence concerning Duchamp may be directed to: |
---|
| 23 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
| 24 | // Postal address: Dr. Matthew Whiting |
---|
| 25 | // Australia Telescope National Facility, CSIRO |
---|
| 26 | // PO Box 76 |
---|
| 27 | // Epping NSW 1710 |
---|
| 28 | // AUSTRALIA |
---|
| 29 | // ----------------------------------------------------------------------- |
---|
[56] | 30 | #include <iostream> |
---|
| 31 | #include <math.h> |
---|
[876] | 32 | #include <stdlib.h> |
---|
[56] | 33 | #include <cpgplot.h> |
---|
[393] | 34 | #include <duchamp/Utils/utils.hh> |
---|
[56] | 35 | |
---|
| 36 | void plotLine(const float slope, const float intercept) |
---|
| 37 | { |
---|
| 38 | float x1, x2, y1, y2; |
---|
| 39 | cpgqwin(&x1,&x2,&y1,&y2); |
---|
| 40 | cpgmove(x1,slope*x1+intercept); |
---|
| 41 | cpgdraw(x2,slope*x2+intercept); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | void lineOfEquality() |
---|
| 45 | { |
---|
| 46 | plotLine(1.,0.); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | void lineOfBestFit(int size, float *x, float *y) |
---|
| 50 | { |
---|
| 51 | float a,b,r,erra,errb; |
---|
| 52 | linear_regression(size,x,y,0,size-1,a,erra,b,errb,r); |
---|
| 53 | plotLine(a,b); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | void plotVertLine(const float xval, 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(¤tColour); |
---|
| 62 | cpgqls(¤tStyle); |
---|
| 63 | if(currentColour!=colour) cpgsci(colour); |
---|
| 64 | if(currentStyle !=style) cpgsls(style); |
---|
| 65 | cpgmove(xval,y1); |
---|
| 66 | cpgdraw(xval,y2); |
---|
| 67 | if(currentColour!=colour) cpgsci(currentColour); |
---|
| 68 | if(currentStyle !=style) cpgsls(currentStyle); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | void plotVertLine(const float xval) |
---|
| 72 | { |
---|
| 73 | int colour,style; |
---|
| 74 | cpgqci(&colour); |
---|
| 75 | cpgqls(&style); |
---|
| 76 | plotVertLine(xval,colour,style); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | void plotVertLine(const float xval, const int colour) |
---|
| 80 | { |
---|
| 81 | int style; |
---|
| 82 | cpgqls(&style); |
---|
| 83 | plotVertLine(xval,colour,style); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | void plotHorizLine(const float yval, const int colour, const int style) |
---|
| 87 | { |
---|
| 88 | float x1, x2, y1, y2; |
---|
| 89 | cpgqwin(&x1,&x2,&y1,&y2); |
---|
| 90 | int currentColour,currentStyle; |
---|
| 91 | cpgqci(¤tColour); |
---|
| 92 | cpgqls(¤tStyle); |
---|
| 93 | if(currentColour!=colour) cpgsci(colour); |
---|
| 94 | if(currentStyle !=style) cpgsls(style); |
---|
| 95 | cpgmove(x1,yval); |
---|
| 96 | cpgdraw(x2,yval); |
---|
| 97 | if(currentColour!=colour) cpgsci(currentColour); |
---|
| 98 | if(currentStyle !=style) cpgsls(currentStyle); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | void plotHorizLine(const float yval) |
---|
| 102 | { |
---|
| 103 | int colour,style; |
---|
| 104 | cpgqci(&colour); |
---|
| 105 | cpgqls(&style); |
---|
| 106 | plotHorizLine(yval,colour,style); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | void plotHorizLine(const float yval, const int colour) |
---|
| 110 | { |
---|
| 111 | int style; |
---|
| 112 | cpgqls(&style); |
---|
| 113 | plotHorizLine(yval,colour,style); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | void lineOfBestFitPB(const int size, const float *x, const float *y) |
---|
| 117 | { |
---|
| 118 | int numSim = int(size * log(float(size)*log(float(size))) ); |
---|
| 119 | float slope=0, intercept=0; |
---|
| 120 | float *xboot = new float[size]; |
---|
| 121 | float *yboot = new float[size]; |
---|
| 122 | for(int sim=0;sim<numSim;sim++){ |
---|
| 123 | for(int i=0;i<size;i++){ |
---|
| 124 | int point = int((1.*rand())/(RAND_MAX+1.0) * size); |
---|
| 125 | xboot[i] = x[point]; |
---|
| 126 | yboot[i] = y[point]; |
---|
| 127 | } |
---|
| 128 | float a,b,r,erra,errb; |
---|
| 129 | linear_regression(size,xboot,yboot,0,size-1,a,erra,b,errb,r); |
---|
| 130 | slope += a; |
---|
| 131 | intercept += b; |
---|
| 132 | } |
---|
[205] | 133 | delete [] xboot; |
---|
| 134 | delete [] yboot; |
---|
[56] | 135 | slope /= float(numSim); |
---|
| 136 | intercept /= float(numSim); |
---|
| 137 | |
---|
| 138 | plotLine(slope,intercept); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | void drawContours(const int size, const float *x, const float *y) |
---|
| 142 | { |
---|
| 143 | float x1, x2, y1, y2; |
---|
| 144 | cpgqwin(&x1,&x2,&y1,&y2); |
---|
| 145 | const int nbin = 30; |
---|
[275] | 146 | // widths of bins in x and y directions: have one bin either side of |
---|
| 147 | // extrema |
---|
| 148 | float binWidthX = (x2 - x1) / float(nbin-2); |
---|
| 149 | float binWidthY = (y2 - y1) / float(nbin-2); |
---|
[56] | 150 | float *binnedarray = new float[nbin*nbin]; |
---|
| 151 | for(int i=0;i<nbin*nbin;i++) binnedarray[i] = 0.; |
---|
| 152 | for(int i=0;i<size;i++){ |
---|
[275] | 153 | int xbin = int( (x[i] - (x1-binWidthX)) / binWidthX ); |
---|
| 154 | int ybin = int( (y[i] - (y1-binWidthY)) / binWidthY ); |
---|
| 155 | int binNum = ybin*nbin + xbin; |
---|
| 156 | if((binNum>=0)&&(binNum<nbin*nbin)) binnedarray[binNum] += 1.; |
---|
[56] | 157 | } |
---|
| 158 | float maxct = binnedarray[0]; |
---|
| 159 | for(int i=1;i<nbin*nbin;i++) if(binnedarray[i]>maxct) maxct=binnedarray[i]; |
---|
[275] | 160 | float tr[6]={x1-3*binWidthX/2,binWidthX,0.,y1-3*binWidthY/2,0.,binWidthY}; |
---|
[56] | 161 | const int ncont = 10; |
---|
| 162 | float *cont = new float[ncont]; |
---|
| 163 | for(int i=0;i<ncont;i++) cont[i] = maxct / pow(2,float(i)/2.); |
---|
| 164 | cpgcont(binnedarray,nbin,nbin,1,nbin,1,nbin,cont,ncont,tr); |
---|
| 165 | delete [] binnedarray; |
---|
| 166 | delete [] cont; |
---|
| 167 | } |
---|