source: tags/release-1.1.12/src/Devel/plottingUtilities.cc

Last change on this file was 393, checked in by MatthewWhiting, 17 years ago

Fixed up headers for trunk as well.

File size: 5.0 KB
Line 
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// -----------------------------------------------------------------------
30#include <iostream>
31#include <math.h>
32#include <cpgplot.h>
33#include <duchamp/Utils/utils.hh>
34
35void plotLine(const float slope, const float intercept)
36{
37  float x1, x2, y1, y2;
38  cpgqwin(&x1,&x2,&y1,&y2);
39  cpgmove(x1,slope*x1+intercept);
40  cpgdraw(x2,slope*x2+intercept);
41}
42
43void lineOfEquality()
44{
45  plotLine(1.,0.);
46}
47
48void lineOfBestFit(int size, float *x, float *y)
49{
50  float a,b,r,erra,errb;
51  linear_regression(size,x,y,0,size-1,a,erra,b,errb,r);
52  plotLine(a,b);
53}
54
55void plotVertLine(const float xval, const int colour, const int style)
56{
57  float x1, x2, y1, y2;
58  cpgqwin(&x1,&x2,&y1,&y2);
59  int currentColour,currentStyle;
60  cpgqci(&currentColour);
61  cpgqls(&currentStyle);
62  if(currentColour!=colour) cpgsci(colour);
63  if(currentStyle !=style)  cpgsls(style);
64  cpgmove(xval,y1);
65  cpgdraw(xval,y2);
66  if(currentColour!=colour) cpgsci(currentColour);
67  if(currentStyle !=style)  cpgsls(currentStyle);
68}
69
70void plotVertLine(const float xval)
71{
72  int colour,style;
73  cpgqci(&colour);
74  cpgqls(&style);
75  plotVertLine(xval,colour,style);
76}
77
78void plotVertLine(const float xval, const int colour)
79{
80  int style;
81  cpgqls(&style);
82  plotVertLine(xval,colour,style);
83}
84
85void plotHorizLine(const float yval, const int colour, const int style)
86{
87  float x1, x2, y1, y2;
88  cpgqwin(&x1,&x2,&y1,&y2);
89  int currentColour,currentStyle;
90  cpgqci(&currentColour);
91  cpgqls(&currentStyle);
92  if(currentColour!=colour) cpgsci(colour);
93  if(currentStyle !=style)  cpgsls(style);
94  cpgmove(x1,yval);
95  cpgdraw(x2,yval);
96  if(currentColour!=colour) cpgsci(currentColour);
97  if(currentStyle !=style)  cpgsls(currentStyle);
98}
99
100void plotHorizLine(const float yval)
101{
102  int colour,style;
103  cpgqci(&colour);
104  cpgqls(&style);
105  plotHorizLine(yval,colour,style);
106}
107
108void plotHorizLine(const float yval, const int colour)
109{
110  int style;
111  cpgqls(&style);
112  plotHorizLine(yval,colour,style);
113}
114
115void lineOfBestFitPB(const int size, const float *x, const float *y)
116{
117  int numSim = int(size * log(float(size)*log(float(size))) );
118  float slope=0, intercept=0;
119  float *xboot = new float[size];
120  float *yboot = new float[size];
121  for(int sim=0;sim<numSim;sim++){
122    for(int i=0;i<size;i++){
123      int point = int((1.*rand())/(RAND_MAX+1.0) * size);
124      xboot[i] = x[point];
125      yboot[i] = y[point];
126    }
127    float a,b,r,erra,errb;
128    linear_regression(size,xboot,yboot,0,size-1,a,erra,b,errb,r);
129    slope += a;
130    intercept += b;
131  }
132  delete [] xboot;
133  delete [] yboot;
134  slope /= float(numSim);
135  intercept /= float(numSim);
136 
137  plotLine(slope,intercept);
138}
139
140void drawContours(const int size, const float *x, const float *y)
141{
142  float x1, x2, y1, y2;
143  cpgqwin(&x1,&x2,&y1,&y2);
144  const int nbin = 30;
145  // widths of bins in x and y directions: have one bin either side of
146  // extrema
147  float binWidthX = (x2 - x1) / float(nbin-2);
148  float binWidthY = (y2 - y1) / float(nbin-2);
149  float *binnedarray = new float[nbin*nbin];
150  for(int i=0;i<nbin*nbin;i++) binnedarray[i] = 0.;
151  for(int i=0;i<size;i++){
152    int xbin = int( (x[i] - (x1-binWidthX)) / binWidthX );
153    int ybin = int( (y[i] - (y1-binWidthY)) / binWidthY );
154    int binNum = ybin*nbin + xbin;
155    if((binNum>=0)&&(binNum<nbin*nbin)) binnedarray[binNum] += 1.;
156  } 
157  float maxct = binnedarray[0];
158  for(int i=1;i<nbin*nbin;i++) if(binnedarray[i]>maxct) maxct=binnedarray[i];
159  float tr[6]={x1-3*binWidthX/2,binWidthX,0.,y1-3*binWidthY/2,0.,binWidthY};
160  const int ncont = 10;
161  float *cont = new float[ncont];
162  for(int i=0;i<ncont;i++) cont[i] = maxct / pow(2,float(i)/2.);
163  cpgcont(binnedarray,nbin,nbin,1,nbin,1,nbin,cont,ncont,tr);
164  delete [] binnedarray;
165  delete [] cont;
166}
Note: See TracBrowser for help on using the repository browser.