Version 2 of How to plot a graph

Updated 2002-10-27 17:11:36

David H. Bigelow wrote in the comp.lang.tcl newsgroup:

The thing to remember with the Tcl Canvas widget is that it starts it's 0 0 at the upper left corner of your display and works down. Knowing that you are looking to do a graph. I have compensated for this so that the graph information is translated to the lower left corner and builds to the right.

Here is the point data I used in a file called: data.txt

 0,0
 10,10
 15,7.5
 20,25
 25,60
 30,30
 35,40
 40,45
 50,60

I have included a couple of variations for computing this. Both are computing the graph such that the origin is the lower left corner of the canvas -- so it looks like a graph:

 #**** PROGRAM 1 -- SIMPLE -- individual lines
 # DECLARE THE SIZE OF THE CANVAS
 set width 100
 set height 100
 canvas .c -width $width -height $height -background yellow
 pack .c

 # READ THE DATA FILE
 set infile [open data.txt]
 set indata [read $infile]
 close $infile

 set count 0
 foreach point $indata {
    if {$count == 0} {
       set oldx [lindex [split $point ,] 0]
       set oldy [expr $height-[lindex [split $point ,] 1]]
       incr count
    }
    if {$count > 0} {
       set newx [lindex [split $point ,] 0]
       set newy [expr $height-[lindex [split $point ,] 1]]
       .c create line $oldx $oldy $newx $newy
       set oldx $newx
       set oldy $newy
    }
 }

PROGRAM 1 is a very simple program to follow, and it gets the job done, although not as efficient as other methods (see below). However, if you are a beginner - this will plot data and quickly. Believe me -- there are more efficient methods.


 #**** PROGRAM 2-- MEDIUM/COMPLEX -- Single line for same data
 # DECLARE THE SIZE OF THE CANVAS
 set width 100
 set height 100
 canvas .c -width $width -height $height -background yellow
 pack .c

 # READ THE DATA FILE
 set infile [open data.txt]
 set indata [read $infile]
 close $infile

 set linecoords {}
 foreach point $indata {
    set x [lindex [split $point ,] 0]
    set y [expr $height-[lindex [split $point ,] 1]]
    lappend linecoords $x $y
 }
 set test ".c create line [join $linecoords]"
 eval $test

PROGRAM 2 is more complicated because it uses more advanved functions of Tcl than the first program. The advantage is that your data set is described in one element. Which may be very helpful if you have multiple data sets to deal with.


RS: Here are some things I'd do differently, just for discussion:

  • linecoords is a list, so it's slightly more efficient to initialize it as such
  • splitting the $point is done twice, once is enough
  • accessing with lindex is OK, but for constant positions, I prefer foreach...break
  • join is not needed, a list referenced in a string will come joined with spaces
 set linecoords [list]
 foreach point $indata {
    foreach {x y} [split $point ,] break
    lappend linecoords $x [expr {$height-$y}]
 }
 eval .c create line $linecoords

SB 2002-10-27: I wanted to be able to plot comma-separated data with one x-value and one or more y-values where spaces basically are ignored so that I can use either csv data from excel or hand-crafted csv files manually modified for human readability. This work is based on the above examples.

The data file data.txt:

 0  ,0   ,1    ,56 ,0
 10 ,10  ,5    ,70 ,10
 15 ,7.5 ,7    ,65 ,15
 20 ,25  ,9    ,10 ,20
 25 ,60  ,19   ,4  ,25
 30 ,30  ,12   ,3  ,30
 35 ,40  ,56   ,16 ,35
 40 ,45  ,12   ,45 ,40
 50 ,60  ,34   ,22 ,50
 60 ,45  ,17   ,23 ,60
 70 ,10  ,15   ,50 ,70

The program:

 #**** PROGRAM 4-- COMPLEX 
 #

 # DECLARE THE SIZE OF THE CANVAS
 set width 100
 set height 100
 canvas .c -width $width -height $height -background white
 pack .c

 # READ THE DATA FILE
 set infile [open "data.txt]

 # knowing that this way of reading a file may be bad
 # I do it anyway and duck for cover
 while {[gets $infile indata] > -1} {
        set i 0
        set el [split $indata ,]

        # looking for something like car and cdr from lisp to process the el list
        foreach {x yvals} [list [lindex $el 0] [lrange $el 1 end]] break

        # process each y-value, the lists are not initialized anymore
        # as I don't know how many datasets I have
        foreach y $yvals {
                lappend linecoords($i) $x [expr {$height-$y}]
                incr i
        }
 }

 close $infile

 # set some colors to be able to separate out each graph
 array set color {
        0        black
        1        red
        2        blue
        3        magenta
 }
 for {set n 0} {$n < $i} {incr n} { 
        eval .c create line $linecoords($n) -fill $color($n)
 }

Arts and crafts of Tcl-Tk programming - See also A little function plotter

Category Application | Category Graphics