Version 8 of A little graph plotter

Updated 2005-03-12 11:53:19 by ulis

ulis, 2003-03-12.

A little script to plot a graph on a canvas. Non-stop scrolling capable. Can be used for real-time plotting.

http://perso.wanadoo.fr/maurice.ulis/tcl/plot.gif


The plotter

  # --------------------------
  #   params
  # --------------------------
  # title
  # canvas width & height
  # delay between plots
  # x = f(t)
  # plot expression
  # initial & final times
  # accuracy
  array set params \
  {
    title     {sin(x)}
    width     400
    height    240
    delay     50
    x         {$t / 50.}
    plot      {sin($x)}
    t0        0
    #         {t1 < t0: non-stop scrolling}
    t1        -1
    accuracy  1.e-2
  }

  # --------------------------
  #   plotting
  # --------------------------
  # computed heights
  set h $params(height)
  set h1 [expr {int($h * 0.5)}]  ;# canvas mid-height
  set h2 [expr {$h1 + 1}]
  set h3 [expr {int($h * 0.4)}]  ;# graph mid-height
  # canvas & bindings
  canvas .c -width $params(width) -height $h \
            -xscrollincrement 1 -bg beige
  pack .c
  bind . <Destroy> { exit }
  # plotting
  wm title . $params(title)
  .c xview scroll $params(t0) unit
  set t $params(t0)
  while {$t != $params(t1)} \
  {
    update
    after $params(delay)
    set x [expr $params(x)]
    set vv [expr $params(plot)]
    set v [expr {int($vv * $h3) + $h1}]
    if {abs($vv) < $params(accuracy)} \
    { 
      .c create text $t 0 -anchor n -text $t -fill gray
      .c create line $t 0 $t $h -fill gray
    }
    .c create line $t $h1 $t $h2 -fill gray
    .c create rectangle $t $v $t $v
    incr t
    if {$t > $params(width)} { .c xview scroll 1 unit }
  }

Another set of parameters

http://perso.wanadoo.fr/maurice.ulis/tcl/plot2.gif

  array set params \
  {
    title     {x^3 - x}
    width     400
    height    240
    delay     50
    x         {$t / 100.0}
    plot      {pow($x,3) - $x}
    t0        -200
    t1        200
    accuracy  1.e-6
  }

See also


Category Example

Category GUI

Category Plotting

Category Widget