plotstat.tcl

Go to the documentation of this file.
00001 /*  plotstat.tcl --*/
00002 /* */
00003 /*     Set of very simple drawing routines, belonging to the statistics*/
00004 /*     package*/
00005 /* */
00006 /*  version 0.1: initial implementation, january 2003*/
00007 
00008 namespace ::math::statistics {}
00009 
00010 /*  plot-scale*/
00011 /*     Set the scale for a plot in the given canvas*/
00012 /* */
00013 /*  Arguments:*/
00014 /*     canvas   Canvas widget to use*/
00015 /*     xmin     Minimum x value*/
00016 /*     xmax     Maximum x value*/
00017 /*     ymin     Minimum y value*/
00018 /*     ymax     Maximum y value*/
00019 /* */
00020 /*  Result:*/
00021 /*     None*/
00022 /* */
00023 /*  Side effect:*/
00024 /*     Array elements set*/
00025 /* */
00026 ret  ::math::statistics::plot-scale ( type canvas , type xmin , type xmax , type ymin , type ymax ) {
00027     variable plot
00028 
00029     if { $xmin == $xmax } { set xmax [expr {1.1*$xmin+1.0}] }
00030     if { $ymin == $ymax } { set ymax [expr {1.1*$ymin+1.0}] }
00031 
00032     set plot($canvas,xmin) $xmin
00033     set plot($canvas,xmax) $xmax
00034     set plot($canvas,ymin) $ymin
00035     set plot($canvas,ymax) $ymax
00036 
00037     set cwidth  [$canvas cget -width]
00038     set cheight [$canvas cget -height]
00039     set cx      20
00040     set cy      20
00041     set cx2     [expr {$cwidth-$cx}]
00042     set cy2     [expr {$cheight-$cy}]
00043 
00044     set plot($canvas,cx)   $cx
00045     set plot($canvas,cy)   $cy
00046 
00047     set plot($canvas,dx)   [expr {($cwidth-2*$cx)/double($xmax-$xmin)}]
00048     set plot($canvas,dy)   [expr {($cheight-2*$cy)/double($ymax-$ymin)}]
00049     set plot($canvas,cx2)  $cx2
00050     set plot($canvas,cy2)  $cy2
00051 
00052     $canvas create line $cx $cy $cx $cy2 $cx2 $cy2 -tag axes
00053 }
00054 
00055 /*  plot-xydata*/
00056 /*     Create a simple XY plot in the given canvas (collection of dots)*/
00057 /* */
00058 /*  Arguments:*/
00059 /*     canvas   Canvas widget to use*/
00060 /*     xdata    Series of independent data*/
00061 /*     ydata    Series of dependent data*/
00062 /*     tag      Tag to give to the plotted data (defaults to xyplot)*/
00063 /* */
00064 /*  Result:*/
00065 /*     None*/
00066 /* */
00067 /*  Side effect:*/
00068 /*     Simple xy graph in the canvas*/
00069 /* */
00070 /*  Note:*/
00071 /*     The tag can be used to manipulate the xy graph*/
00072 /* */
00073 ret  ::math::statistics::plot-xydata ( type canvas , type xdata , type ydata , optional tag =xyplot ) {
00074     PlotXY $canvas points $tag $xdata $ydata
00075 }
00076 
00077 /*  plot-xyline*/
00078 /*     Create a simple XY plot in the given canvas (continuous line)*/
00079 /* */
00080 /*  Arguments:*/
00081 /*     canvas   Canvas widget to use*/
00082 /*     xdata    Series of independent data*/
00083 /*     ydata    Series of dependent data*/
00084 /*     tag      Tag to give to the plotted data (defaults to xyplot)*/
00085 /* */
00086 /*  Result:*/
00087 /*     None*/
00088 /* */
00089 /*  Side effect:*/
00090 /*     Simple xy graph in the canvas*/
00091 /* */
00092 /*  Note:*/
00093 /*     The tag can be used to manipulate the xy graph*/
00094 /* */
00095 ret  ::math::statistics::plot-xyline ( type canvas , type xdata , type ydata , optional tag =xyplot ) {
00096     PlotXY $canvas line $tag $xdata $ydata
00097 }
00098 
00099 /*  plot-tdata*/
00100 /*     Create a simple XY plot in the given canvas (the index in the list*/
00101 /*     is the horizontal coordinate; points)*/
00102 /* */
00103 /*  Arguments:*/
00104 /*     canvas   Canvas widget to use*/
00105 /*     tdata    Series of dependent data*/
00106 /*     tag      Tag to give to the plotted data (defaults to xyplot)*/
00107 /* */
00108 /*  Result:*/
00109 /*     None*/
00110 /* */
00111 /*  Side effect:*/
00112 /*     Simple xy graph in the canvas*/
00113 /* */
00114 /*  Note:*/
00115 /*     The tag can be used to manipulate the xy graph*/
00116 /* */
00117 ret  ::math::statistics::plot-tdata ( type canvas , type tdata , optional tag =xyplot ) {
00118     PlotXY $canvas points $tag {} $tdata
00119 }
00120 
00121 /*  plot-tline*/
00122 /*     Create a simple XY plot in the given canvas (the index in the list*/
00123 /*     is the horizontal coordinate; line)*/
00124 /* */
00125 /*  Arguments:*/
00126 /*     canvas   Canvas widget to use*/
00127 /*     tdata    Series of dependent data*/
00128 /*     tag      Tag to give to the plotted data (defaults to xyplot)*/
00129 /* */
00130 /*  Result:*/
00131 /*     None*/
00132 /* */
00133 /*  Side effect:*/
00134 /*     Simple xy graph in the canvas*/
00135 /* */
00136 /*  Note:*/
00137 /*     The tag can be used to manipulate the xy graph*/
00138 /* */
00139 ret  ::math::statistics::plot-tline ( type canvas , type tdata , optional tag =xyplot ) {
00140     PlotXY $canvas line $tag {} $tdata
00141 }
00142 
00143 /*  PlotXY*/
00144 /*     Create a simple XY plot (points or lines) in the given canvas*/
00145 /* */
00146 /*  Arguments:*/
00147 /*     canvas   Canvas widget to use*/
00148 /*     type     Type: points or line*/
00149 /*     tag      Tag to give to the plotted data*/
00150 /*     xdata    Series of independent data (if empty: index used instead)*/
00151 /*     ydata    Series of dependent data*/
00152 /* */
00153 /*  Result:*/
00154 /*     None*/
00155 /* */
00156 /*  Side effect:*/
00157 /*     Simple xy graph in the canvas*/
00158 /* */
00159 /*  Note:*/
00160 /*     This is the actual routine*/
00161 /* */
00162 ret  ::math::statistics::PlotXY ( type canvas , type type , type tag , type xdata , type ydata ) {
00163     variable plot
00164 
00165     if { ![info exists plot($canvas,xmin)] } {
00166     return -code error -errorcode "No scaling given for canvas $canvas"
00167     }
00168 
00169     set xmin $plot($canvas,xmin)
00170     set xmax $plot($canvas,xmax)
00171     set ymin $plot($canvas,ymin)
00172     set ymax $plot($canvas,ymax)
00173     set dx   $plot($canvas,dx)
00174     set dy   $plot($canvas,dy)
00175     set cx   $plot($canvas,cx)
00176     set cy   $plot($canvas,cy)
00177     set cx2  $plot($canvas,cx2)
00178     set cy2  $plot($canvas,cy2)
00179 
00180     set plotpoints [expr {$type == "points"}]
00181     set xpresent   [expr {[llength $xdata] > 0}]
00182     set idx        0
00183     set coords     {}
00184 
00185     foreach y $ydata {
00186     if { $xpresent } {
00187         set x [lindex $xdata $idx]
00188     } else {
00189         set x $idx
00190     }
00191     incr idx
00192 
00193     if { $x == {}    } continue
00194     if { $y == {}    } continue
00195     if { $x >  $xmax } continue
00196     if { $x <  $xmin } continue
00197     if { $y >  $ymax } continue
00198     if { $y <  $ymin } continue
00199 
00200     if { $plotpoints } {
00201         set xc [expr {$cx+$dx*($x-$xmin)-2}]
00202         set yc [expr {$cy2-$dy*($y-$ymin)-2}]
00203         set xc2 [expr {$xc+4}]
00204         set yc2 [expr {$yc+4}]
00205         $canvas create oval $xc $yc $xc2 $yc2 -tag $tag -fill black
00206     } else {
00207         set xc [expr {$cx+$dx*($x-$xmin)}]
00208         set yc [expr {$cy2-$dy*($y-$ymin)}]
00209         lappend coords $xc $yc
00210     }
00211     }
00212 
00213     if { ! $plotpoints } {
00214     $canvas create line $coords -tag $tag
00215     }
00216 }
00217 
00218 /*  plot-histogram*/
00219 /*     Create a simple histogram in the given canvas*/
00220 /* */
00221 /*  Arguments:*/
00222 /*     canvas   Canvas widget to use*/
00223 /*     counts   Series of bucket counts*/
00224 /*     limits   Series of upper limits for the buckets*/
00225 /*     tag      Tag to give to the plotted data (defaults to xyplot)*/
00226 /* */
00227 /*  Result:*/
00228 /*     None*/
00229 /* */
00230 /*  Side effect:*/
00231 /*     Simple histogram in the canvas*/
00232 /* */
00233 /*  Note:*/
00234 /*     The number of limits determines how many bars are drawn,*/
00235 /*     the number of counts that is expected is one larger. The*/
00236 /*     lower and upper limits of the first and last bucket are*/
00237 /*     taken to be equal to the scale's extremes*/
00238 /* */
00239 ret  ::math::statistics::plot-histogram ( type canvas , type counts , type limits , optional tag =xyplot ) {
00240     variable plot
00241 
00242     if { ![info exists plot($canvas,xmin)] } {
00243     return -code error -errorcode DATA "No scaling given for canvas $canvas"
00244     }
00245 
00246     if { ([llength $counts]-[llength $limits]) != 1 } {
00247     return -code error -errorcode ARG \
00248         "Number of counts does not correspond to number of limits"
00249     }
00250 
00251     set xmin $plot($canvas,xmin)
00252     set xmax $plot($canvas,xmax)
00253     set ymin $plot($canvas,ymin)
00254     set ymax $plot($canvas,ymax)
00255     set dx   $plot($canvas,dx)
00256     set dy   $plot($canvas,dy)
00257     set cx   $plot($canvas,cx)
00258     set cy   $plot($canvas,cy)
00259     set cx2  $plot($canvas,cx2)
00260     set cy2  $plot($canvas,cy2)
00261 
00262     #
00263     # Construct a sufficiently long list of x-coordinates
00264     #
00265     set xdata [concat $xmin $limits $xmax]
00266 
00267     set idx   0
00268     foreach x $xdata y $counts {
00269     incr idx
00270 
00271     if { $y == {}    } continue
00272 
00273     set x1 $x
00274     if { $x <  $xmin } { set x1 $xmin }
00275     if { $x >  $xmax } { set x1 $xmax }
00276 
00277     if { $y >  $ymax } { set y $ymax }
00278     if { $y <  $ymin } { set y $ymin }
00279 
00280     set x2  [lindex $xdata $idx]
00281     if { $x2 <  $xmin } { set x2 $xmin }
00282     if { $x2 >  $xmax } { set x2 $xmax }
00283 
00284     set xc  [expr {$cx+$dx*($x1-$xmin)}]
00285     set xc2 [expr {$cx+$dx*($x2-$xmin)}]
00286     set yc  [expr {$cy2-$dy*($y-$ymin)}]
00287     set yc2 $cy2
00288 
00289     $canvas create rectangle $xc $yc $xc2 $yc2 -tag $tag -fill blue
00290     }
00291 }
00292 
00293 /* */
00294 /*  Simple test code*/
00295 /* */
00296 if { [info exists ::argv0] && ([file tail [info script]] == [file tail $::argv0]) } {
00297 
00298      xdata =  {1 2 3 4 5 10 20 6 7 8 1 3 4 5 6 7}
00299      ydata =  {2 3 4 5 6 10 20 7 8 1 3 4 5 6 7 1}
00300 
00301     canvas .c
00302     canvas .c2
00303     pack   .c .c2 -side top -fill both
00304     ::math::statistics::plot-scale .c  0 10 0 10
00305     ::math::statistics::plot-scale .c2 0 20 0 10
00306 
00307     ::math::statistics::plot-xydata .c  $xdata $ydata
00308     ::math::statistics::plot-xyline .c  $xdata $ydata
00309     ::math::statistics::plot-histogram .c2 {1 3 2 0.1 4 2} {-1 3 10 11 23}
00310     ::math::statistics::plot-tdata  .c2 $xdata
00311     ::math::statistics::plot-tline  .c2 $xdata
00312 }
00313 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1