Root/nanonote-files/example-files/data/Examples/lua-plplot-examples/x01.lua

1--[[ $Id: x01.lua 9414 2009-01-29 22:48:54Z airwin $
2
3    Simple line plot and multiple windows demo.
4
5  Copyright (C) 2008 Werner Smekal
6
7  This file is part of PLplot.
8
9  PLplot is free software you can redistribute it and/or modify
10  it under the terms of the GNU General Library Public License as published
11  by the Free Software Foundation either version 2 of the License, or
12  (at your option) any later version.
13
14  PLplot is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Library General Public License for more details.
18
19  You should have received a copy of the GNU Library General Public License
20  along with PLplot if not, write to the Free Software
21  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22--]]
23
24-- initialise Lua bindings for PLplot examples.
25dofile("plplot_examples.lua")
26
27-- Variables and data arrays used by plot generators
28x = {}
29y = {}
30xs = {}
31ys = {}
32
33fontset = 1
34f_name = ""
35
36
37function plot1(do_test)
38  for i = 1, 60 do
39    x[i] = xoff + xscale * (i) / 60
40    y[i] = yoff + yscale * x[i]^2
41  end
42
43  xmin = x[1]
44  xmax = x[60]
45  ymin = y[1]
46  ymax = y[60]
47
48  for i = 1, 6 do
49    xs[i] = x[(i-1) * 10 + 4]
50    ys[i] = y[(i-1) * 10 + 4]
51  end
52
53  -- Set up the viewport and window using PLENV. The range in X is
54  -- 0.0 to 6.0, and the range in Y is 0.0 to 30.0. The axes are
55  -- scaled separately (just = 0), and we just draw a labelled
56  -- box (axis = 0).
57  pl.col0(1)
58  pl.env(xmin, xmax, ymin, ymax, 0, 0)
59  pl.col0(2)
60  pl.lab("(x)", "(y)", "#frPLplot Example 1 - y=x#u2")
61
62  -- Plot the data points
63  pl.col0(4)
64  pl.poin(xs, ys, 9)
65
66  -- Draw the line through the data
67  pl.col0(3)
68  pl.line(x, y)
69end
70
71
72function plot2()
73  -- Set up the viewport and window using PLENV. The range in X is -2.0 to
74  -- 10.0, and the range in Y is -0.4 to 2.0. The axes are scaled separately
75  --(just = 0), and we draw a box with axes (axis = 1).
76  pl.col0(1)
77  pl.env(-2, 10, -0.4, 1.2, 0, 1)
78  pl.col0(2)
79  pl.lab("(x)", "sin(x)/x", "#frPLplot Example 1 - Sinc Function")
80
81  -- Fill up the arrays
82  for i=1, 100 do
83    x[i] = (i - 20) / 6
84    y[i] = 1
85    if x[i] ~= 0 then y[i] = math.sin(x[i])/x[i] end
86  end
87
88  -- Draw the line
89  pl.col0(3)
90  pl.wid(2)
91  pl.line(x, y)
92  pl.wid(1)
93end
94
95
96function plot3()
97  space0 = { }
98  mark0 = { }
99  space1 = { 1500 }
100  mark1 = { 1500 }
101
102  -- For the final graph we wish to override the default tick intervals, and
103  --so do not use plenv().
104  pl.adv(0)
105
106  -- Use standard viewport, and define X range from 0 to 360 degrees, Y range
107  --from -1.2 to 1.2.
108  pl.vsta()
109  pl.wind(0, 360, -1.2, 1.2)
110
111  -- Draw a box with ticks spaced 60 degrees apart in X, and 0.2 in Y.
112  pl.col0(1)
113  pl.box("bcnst", 60, 2, "bcnstv", 0.2, 2)
114
115  -- Superimpose a dashed line grid, with 1.5 mm marks and spaces.
116  -- plstyl expects a pointer!
117  pl.styl(mark1, space1)
118  pl.col0(2)
119  pl.box("g", 30, 0, "g", 0.2, 0)
120  pl.styl(mark0, space0)
121
122  pl.col0(3)
123  pl.lab("Angle (degrees)", "sine", "#frPLplot Example 1 - Sine function")
124
125  for i=1, 101 do
126    x[i] = 3.6*(i-1)
127    y[i] = math.sin(x[i]*math.pi/180)
128  end
129
130  pl.col0(4)
131  pl.line(x, y)
132end
133
134
135----------------------------------------------------------------------------
136-- main
137--
138-- Generates several simple line plots. Demonstrates:
139-- - subwindow capability
140-- - setting up the window, drawing plot, and labelling
141-- - changing the color
142-- - automatic axis rescaling to exponential notation
143-- - placing the axes in the middle of the box
144-- - gridded coordinate axes
145----------------------------------------------------------------------------
146
147-- plplot initialization
148
149-- Parse and process command line arguments
150pl.parseopts(arg, pl.PL_PARSE_FULL)
151
152-- Get version number, just for kicks
153ver=pl.gver()
154print("PLplot library version: " .. ver)
155
156-- Initialize plplot
157-- Divide page into 2x2 plots
158-- Note: calling plstar replaces separate calls to plssub and plinit
159pl.star(2,2)
160
161-- Select font set as per input flag
162if fontset ~= 0 then
163    pl.fontld(1)
164else
165    pl.fontld(0)
166end
167  
168-- Set up the data
169-- Original case
170xscale = 6
171yscale = 1
172xoff = 0
173yoff = 0
174
175-- Do a plot
176plot1(0)
177
178-- Set up the data
179xscale = 1
180yscale = 0.0014
181yoff = 0.0185
182
183-- Do a plot
184digmax = 5
185pl.syax(digmax, 0)
186
187plot1(1)
188plot2()
189plot3()
190
191-- Show how to save a plot:
192-- Open a new device, make it current, copy parameters,
193-- and replay the plot buffer
194if f_name~="" then -- command line option '-save filename'
195  print("The current plot was saved in color Postscript under the name " .. f_name .. ".\n")
196  cur_strm = pl.gstrm() -- get current stream
197  new_strm = pl.mkstrm() -- create a new one
198
199  pl.sfnam(f_name) -- file name
200  pl.sdev("psc") -- device type
201
202  pl.cpstrm(cur_strm, 0) -- copy old stream parameters to new stream
203  pl.replot() -- do the save by replaying the plot buffer
204  pl.plend1() -- finish the device
205
206  pl.sstrm(cur_strm) -- return to previous stream
207end
208
209-- Don't forget to call plend() to finish off!
210pl.plend()
211

Archive Download this file



interactive