rogerdpack-gnuplot 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/gnuplot.rb +283 -0
- metadata +53 -0
data/lib/gnuplot.rb
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
# Methods and variables for interacting with the gnuplot process. Most of
|
|
2
|
+
# these methods are for sending data to a gnuplot process, not for reading from
|
|
3
|
+
# it. Most of the methods are implemented as added methods to the built in
|
|
4
|
+
# classes.
|
|
5
|
+
|
|
6
|
+
require 'matrix'
|
|
7
|
+
|
|
8
|
+
module Gnuplot
|
|
9
|
+
|
|
10
|
+
# Trivial implementation of the which command that uses the PATH environment
|
|
11
|
+
# variable to attempt to find the given application. The application must
|
|
12
|
+
# be executable and reside in one of the directories in the PATH environment
|
|
13
|
+
# to be found. The first match that is found will be returned.
|
|
14
|
+
#
|
|
15
|
+
# bin [String] The name of the executable to search for.
|
|
16
|
+
#
|
|
17
|
+
# Return the full path to the first match or nil if no match is found.
|
|
18
|
+
#
|
|
19
|
+
def Gnuplot.which ( bin )
|
|
20
|
+
return bin if File::executable? bin
|
|
21
|
+
|
|
22
|
+
path = ENV['PATH'] # || ENV['WHAT_EVER_WINDOWS_PATH_VAR_IS']
|
|
23
|
+
path.split(File::PATH_SEPARATOR).each do |dir|
|
|
24
|
+
candidate = File::join dir, bin.strip
|
|
25
|
+
return candidate if File::executable? candidate
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# This is an implementation that works when the which command is
|
|
29
|
+
# available.
|
|
30
|
+
#
|
|
31
|
+
# IO.popen("which #{bin}") { |io| return io.readline.chomp }
|
|
32
|
+
|
|
33
|
+
return nil
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Find the path to the gnuplot executable. The name of the executable can
|
|
37
|
+
# be specified using the RB_GNUPLOT environment variable but will default to
|
|
38
|
+
# the command 'gnuplot'.
|
|
39
|
+
#
|
|
40
|
+
# persist [bool] Add the persist flag to the gnuplot executable
|
|
41
|
+
#
|
|
42
|
+
# Return the path to the gnuplot executable or nil if one cannot be found.
|
|
43
|
+
def Gnuplot.gnuplot( persist = true )
|
|
44
|
+
cmd = which( ENV['RB_GNUPLOT'] || 'gnuplot' )
|
|
45
|
+
cmd += " -persist" if persist
|
|
46
|
+
cmd
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Open a gnuplot process that exists in the current PATH. If the persist
|
|
50
|
+
# flag is true then the -persist flag is added to the command line. The
|
|
51
|
+
# path to the gnuplot executable is determined using the 'which' command.
|
|
52
|
+
#
|
|
53
|
+
# See the gnuplot documentation for information on the persist flag.
|
|
54
|
+
#
|
|
55
|
+
# <b>todo</b> Add a method to pass the gnuplot path to the function.
|
|
56
|
+
|
|
57
|
+
def Gnuplot.open( persist=true )
|
|
58
|
+
cmd = Gnuplot.gnuplot( persist ) or raise 'gnuplot not found'
|
|
59
|
+
IO::popen( cmd, "w") { |io| yield io }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
# Holds command information and performs the formatting of that command
|
|
64
|
+
# information to a Gnuplot process. When constructing a new plot for
|
|
65
|
+
# gnuplot, this is the first object that must be instantiated. On this
|
|
66
|
+
# object set the various properties and add data sets.
|
|
67
|
+
|
|
68
|
+
class Plot
|
|
69
|
+
attr_accessor :cmd, :data, :sets
|
|
70
|
+
|
|
71
|
+
QUOTED = [ "title", "output", "xlabel", "ylabel" ]
|
|
72
|
+
|
|
73
|
+
def initialize (io = nil, cmd = "plot")
|
|
74
|
+
@cmd = cmd
|
|
75
|
+
@sets = []
|
|
76
|
+
@data = []
|
|
77
|
+
yield self if block_given?
|
|
78
|
+
|
|
79
|
+
io << to_gplot if io
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Invoke the set method on the plot using the name of the invoked method
|
|
83
|
+
# as the set variable and any arguments that have been passed as the
|
|
84
|
+
# value. See the +set+ method for more details.
|
|
85
|
+
|
|
86
|
+
def method_missing( methId, *args )
|
|
87
|
+
set methId.id2name, *args
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
# Set a variable to the given value. +Var+ must be a gnuplot variable and
|
|
92
|
+
# +value+ must be the value to set it to. Automatic quoting will be
|
|
93
|
+
# performed if the variable requires it.
|
|
94
|
+
#
|
|
95
|
+
# This is overloaded by the +method_missing+ method so see that for more
|
|
96
|
+
# readable code.
|
|
97
|
+
|
|
98
|
+
def set ( var, value = "" )
|
|
99
|
+
value = "'#{value}'" if QUOTED.include? var unless value =~ /^'.*'$/
|
|
100
|
+
@sets << [ var, value ]
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
# Return the current value of the variable. This will return the setting
|
|
105
|
+
# that is currently in the instance, not one that's been given to a
|
|
106
|
+
# gnuplot process.
|
|
107
|
+
|
|
108
|
+
def [] ( var )
|
|
109
|
+
v = @sets.assoc( var )
|
|
110
|
+
v[1] || nil
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def add_data ( ds )
|
|
115
|
+
@data << ds
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def to_gplot (io = "")
|
|
120
|
+
@sets.each { |var, val| io << "set #{var} #{val}\n" }
|
|
121
|
+
|
|
122
|
+
if @data.size > 0 then
|
|
123
|
+
io << @cmd << " " << @data.collect { |e| e.plot_args }.join(", ")
|
|
124
|
+
io << "\n"
|
|
125
|
+
|
|
126
|
+
v = @data.collect { |ds| ds.to_gplot }
|
|
127
|
+
io << v.compact.join("e\n")
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
io
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
class SPlot < Plot
|
|
136
|
+
|
|
137
|
+
def initialize (io = nil, cmd = "splot")
|
|
138
|
+
super
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def to_gplot (io = "")
|
|
142
|
+
@sets.each { |var, val| io << "set #{var} #{val}\n" }
|
|
143
|
+
|
|
144
|
+
if @data.size > 0 then
|
|
145
|
+
io << @cmd << " "
|
|
146
|
+
io << @data.collect { |e| e.plot_args }.join(", ")
|
|
147
|
+
io << "\n"
|
|
148
|
+
|
|
149
|
+
@data.each do |ds|
|
|
150
|
+
io << ds.to_gsplot << "e\n"
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
io
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
# Container for a single dataset being displayed by gnuplot. Each object
|
|
162
|
+
# has a reference to the actual data being plotted as well as settings that
|
|
163
|
+
# control the "plot" command. The data object must support the to_gplot
|
|
164
|
+
# command.
|
|
165
|
+
#
|
|
166
|
+
# +data+ The data that will be plotted. The only requirement is that the
|
|
167
|
+
# object understands the to_gplot method.
|
|
168
|
+
#
|
|
169
|
+
# The following attributes correspond to their related string in the gnuplot
|
|
170
|
+
# command. See the gnuplot documentation for more information on this.
|
|
171
|
+
#
|
|
172
|
+
# title, with
|
|
173
|
+
#
|
|
174
|
+
# @todo Use the delegator to delegate to the data property.
|
|
175
|
+
|
|
176
|
+
class DataSet
|
|
177
|
+
attr_accessor :title, :with, :using, :data, :linewidth, :matrix
|
|
178
|
+
|
|
179
|
+
def initialize (data = nil)
|
|
180
|
+
@data = data
|
|
181
|
+
yield self if block_given?
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def notitle
|
|
185
|
+
@title = "notitle"
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def plot_args (io = "")
|
|
189
|
+
|
|
190
|
+
# Order of these is important or gnuplot barfs on 'em
|
|
191
|
+
|
|
192
|
+
io << ( (@data.instance_of? String) ? @data : "'-'" )
|
|
193
|
+
|
|
194
|
+
io << " using #{@using}" if @using
|
|
195
|
+
|
|
196
|
+
io << case @title
|
|
197
|
+
when /notitle/ then " notitle"
|
|
198
|
+
when nil then ""
|
|
199
|
+
else " title '#{@title}'"
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
io << " matrix" if @matrix
|
|
203
|
+
io << " with #{@with}" if @with
|
|
204
|
+
io << " linewidth #{@linewidth}" if @linewidth
|
|
205
|
+
io
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def to_gplot
|
|
209
|
+
case @data
|
|
210
|
+
when nil then nil
|
|
211
|
+
when String then nil
|
|
212
|
+
else @data.to_gplot
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def to_gsplot
|
|
217
|
+
case @data
|
|
218
|
+
when nil then nil
|
|
219
|
+
when String then nil
|
|
220
|
+
else @data.to_gsplot
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
class Array
|
|
228
|
+
def to_gplot
|
|
229
|
+
if ( self[0].kind_of? Array ) then
|
|
230
|
+
tmp = self[0].zip( *self[1..-1] )
|
|
231
|
+
tmp.collect { |a| a.join(" ") }.join("\n") + "\ne"
|
|
232
|
+
elsif ( self[0].kind_of? Numeric ) then
|
|
233
|
+
s = ""
|
|
234
|
+
self.length.times { |i| s << "#{self[i]}\n" }
|
|
235
|
+
s
|
|
236
|
+
else
|
|
237
|
+
self[0].zip( *self[1..-1] ).to_gplot
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def to_gsplot
|
|
242
|
+
f = ""
|
|
243
|
+
|
|
244
|
+
if ( self[0].kind_of? Array ) then
|
|
245
|
+
x = self[0]
|
|
246
|
+
y = self[1]
|
|
247
|
+
d = self[2]
|
|
248
|
+
|
|
249
|
+
x.each_with_index do |xv, i|
|
|
250
|
+
y.each_with_index do |yv, j|
|
|
251
|
+
f << [ xv, yv, d[i][j] ].join(" ") << "\n"
|
|
252
|
+
end
|
|
253
|
+
# f << "\n"
|
|
254
|
+
end
|
|
255
|
+
elsif ( self[0].kind_of? Numeric ) then
|
|
256
|
+
self.length.times do |i| f << "#{self[i]}\n" end
|
|
257
|
+
else
|
|
258
|
+
self[0].zip( *self[1..-1] ).to_gsplot
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
f
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
class Matrix
|
|
266
|
+
def to_gplot (x = nil, y = nil)
|
|
267
|
+
xgrid = x || (0...self.column_size).to_a
|
|
268
|
+
ygrid = y || (0...self.row_size).to_a
|
|
269
|
+
|
|
270
|
+
f = ""
|
|
271
|
+
ygrid.length.times do |j|
|
|
272
|
+
y = ygrid[j]
|
|
273
|
+
xgrid.length.times do |i|
|
|
274
|
+
if ( self[j,i] ) then
|
|
275
|
+
f << "#{xgrid[i]} #{y} #{self[j,i]}\n"
|
|
276
|
+
end
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
f
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rogerdpack-gnuplot
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 2.2.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Gordon James Miller, Roger Pack
|
|
8
|
+
autorequire: gnuplot.rb
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2009-07-08 00:00:00 -07:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies: []
|
|
15
|
+
|
|
16
|
+
description: Utility library to aid in interacting with gnuplot
|
|
17
|
+
email: rogerpack2005@gmail.com
|
|
18
|
+
executables: []
|
|
19
|
+
|
|
20
|
+
extensions: []
|
|
21
|
+
|
|
22
|
+
extra_rdoc_files: []
|
|
23
|
+
|
|
24
|
+
files:
|
|
25
|
+
- lib/gnuplot.rb
|
|
26
|
+
has_rdoc: false
|
|
27
|
+
homepage: http://github.com/rogerdpack/ruby_gnuplot/tree/master
|
|
28
|
+
post_install_message:
|
|
29
|
+
rdoc_options: []
|
|
30
|
+
|
|
31
|
+
require_paths:
|
|
32
|
+
- lib
|
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
34
|
+
requirements:
|
|
35
|
+
- - ">="
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: "0"
|
|
38
|
+
version:
|
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
40
|
+
requirements:
|
|
41
|
+
- - ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: "0"
|
|
44
|
+
version:
|
|
45
|
+
requirements: []
|
|
46
|
+
|
|
47
|
+
rubyforge_project:
|
|
48
|
+
rubygems_version: 1.2.0
|
|
49
|
+
signing_key:
|
|
50
|
+
specification_version: 2
|
|
51
|
+
summary: Utility library to aid in interacting with gnuplot
|
|
52
|
+
test_files: []
|
|
53
|
+
|