00001
00002
00003
00004
00005
00006
00007
00008 namespace ::math::statistics {}
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
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
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
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
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 ret ::math::statistics::plot-tdata ( type canvas , type tdata , optional tag =xyplot ) {
00118 PlotXY $canvas points $tag {} $tdata
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 ret ::math::statistics::plot-tline ( type canvas , type tdata , optional tag =xyplot ) {
00140 PlotXY $canvas line $tag {} $tdata
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
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
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
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
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