File: gnuplot.sal

package info (click to toggle)
nyquist 3.20%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 58,008 kB
  • sloc: ansic: 74,743; lisp: 17,929; java: 10,723; cpp: 6,690; sh: 171; xml: 58; makefile: 40; python: 15
file content (162 lines) | stat: -rw-r--r-- 5,411 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
;; interface to gnuplot
;; See example calls below (gptest, gphist-test)

variable *gp-outf*, *gp-data*, *gp-xlabel*, *gp-ylabel*, *gp-driver*,
         *gp-output*, *gp-command*, *gp-curve*, *gp-sep*, *gp-title*,
         *gp-style*, *label*, *gpx*, *gpy*, *gp-fontspec* = "", 
         *gp-xtics-fontspec*, *gp-ytics-fontspec*

function gp-init(filename, driver: "aqua", output: nil, xlabel: nil, 
                 ylabel: nil, title: nil , style: nil, xrange: nil, yrange: nil,
                 fontsize: nil, xticsize: nil, yticsize: nil)
  begin
    set *gp-outf* = open(filename, direction: :output),
        *gp-data* = nil,
        *gp-xlabel* = xlabel,
        *gp-ylabel* = ylabel,
        *gp-driver* = driver,
        *gp-command* = "plot",
        *gp-sep* = "",
        *gp-output* = output,
        *gp-title* = title,
        *gp-style* = style
    if xrange then
      set xrange = format(nil, " [~A:~A]", first(xrange), second(xrange))
    else set xrange = ""
    if yrange then
      begin
        if string=(xrange, "") then set xrange = " []"
        set yrange = format(nil, " [~A:~A]", first(yrange), second(yrange))
      end
    else set yrange = ""
    if fontsize then
      set *gp-fontspec* = format(nil, " font \",~A\"", fontsize)
    ;; NOTE: In my gnu-plot, "Times-Roman,24" works, but ",24" does not.
    if xticsize then
      set *gp-xtics-fontspec* = format(nil, "Times-Roman,~A", xticsize)
    if yticsize then
      set *gp-ytics-fontspec* = format(nil, "Times-Roman,~A", yticsize)
    set *gp-command* = strcat("plot", xrange, yrange)    
  end


function gp-newcurve(title: "", color: nil , label: nil , x: nil , y: nil )
  begin
    set *label* = label
    set *gpx* = x
    set *gpy* = y
    if *label* then
      begin
        exec format(*gp-outf*, "set label \"~A\"", *label*)
        exec format(*gp-outf*, " ")
        exec format(*gp-outf*, "at  ~A", *gpx*)
        exec format(*gp-outf*, " , ")
        exec format(*gp-outf*, "~A~%", *gpy*)
      end
    set *gp-curve* = nil                          
    if *gp-style* = :histogram then
      set *gp-command* = strcat(*gp-command*, *gp-sep*, " '-' w boxes title \"",
                                  title, "\" lt -1")
    else if *gp-style* = :scatter then
      set *gp-command* = strcat(*gp-command*, *gp-sep*, " '-' title \"", 
                                  title, "\"")            
    else
      set *gp-command* = strcat(*gp-command*, *gp-sep*, " '-' w l title \"", 
                                  title, "\"")            
    set *gp-sep* = ","
    if color  then
      set *gp-command* = strcat(*gp-command* ," lc rgbcolor \"", color, "\"")
  end


function gp-point(x, y)
  set *gp-curve* @= list(x, y)

function gp-label(text, x, y)
   exec format(*gp-outf*, "set label \"~A\" at ~A,~A~%", text, x, y)


function gp-endcurve()
  begin
    ;display "gp-endcurve", *gp-curve*, *gp-data*
    set *gp-curve* = reverse(*gp-curve*),
        *gp-data* @= *gp-curve*
  end


function gp-endplot()
  begin
    set *gp-data* = reverse(*gp-data*)
    exec format(*gp-outf*, "set term ~A~A~%", *gp-driver*, *gp-fontspec*)
    if *gp-output* then
      exec format(*gp-outf*, "set output \"~A\"~%", *gp-output*)
    if *gp-xlabel* then
      exec format(*gp-outf*, "set xlabel \"~A\"~%", *gp-xlabel*)
    if *gp-ylabel* then
      exec format(*gp-outf*, "set ylabel \"~A\"~%", *gp-ylabel*)
    if *gp-xtics-fontspec* then
      exec format(*gp-outf*, "set xtics font \"~A\"~%", *gp-xtics-fontspec*)
    if *gp-ytics-fontspec* then
      exec format(*gp-outf*, "set ytics font \"~A\"~%", *gp-ytics-fontspec*)
    if *gp-title* then
      exec format(*gp-outf*, "set title \"~A\"~%", *gp-title*)
   
    
    if *gp-style* = :histogram then
      begin
        exec format(*gp-outf*, "set style data histograms ~%" )
        exec format(*gp-outf*, "set style fill solid 0.2 border -1 ~%" )
      end   
    exec format(*gp-outf*, "~A~%", *gp-command*)
    loop
      for curve in *gp-data*
      loop
        for pair in curve
        exec format(*gp-outf*, "~A ~A~%", first(pair), second(pair))
        
      end
      exec format(*gp-outf*, "e~%")
    end
    exec close(*gp-outf*)
  end
  
;; gptest -- exercise some features
function gptest()
  begin
    exec gp-init("testplot.txt", xlabel: "Time", ylabel: "Tempo", 
                 title: "This Is a Test", fontsize: 12)
    exec gp-newcurve(title: "curve-1", color: "black"  )
    loop
      for i from 0 below 50
      exec gp-point(i, 90 + real-random(-4, 4) + 10 * sin(i / 30.0))
    end
    exec gp-endcurve()

    exec gp-newcurve(title: "smooth" , label: "test label" , x: 30 , y: 90 ) ; default color
    loop
      for i from 0 below 50
      exec gp-point(i, 90 + 10 * sin(i / 30.0))
    end
    exec gp-endcurve()

    exec gp-label("Random Label", 5, 102)

    exec gp-endplot()
  end

;; gphist-test -- exercise some features and make a histogram
function gphist-test()
  begin
    exec gp-init("testhist.txt", xlabel: "delta", ylabel: "N", 
                 title: "This Is a Histogram Example", style: :histogram,
                 ; note: bars are "fat", so increase the range to avoid clipping
                 xrange: {-10.5 10.5},
                 yrange: {0 1.5})
    exec gp-newcurve()
    loop 
      for i from -10 to 10
      exec gp-point(i, cos(i / 10.0) + 0.13)
    end
    exec gp-endcurve()
    exec gp-endplot()
  end