numo-gnuplot 0.1.1 → 0.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4e865eb2bf31c1edbd04ceddc258b75df8aab368
4
- data.tar.gz: ee119b110229d70d890b908b568aaa785fda9fbd
3
+ metadata.gz: 8bad6abaa98a0a9d7359396665513a4c5d89dd67
4
+ data.tar.gz: 24b90ba847429a8af4b6afe4c8c57ef77ac286fc
5
5
  SHA512:
6
- metadata.gz: c31f7b733ecaa2a41cbb650338657177e781d2a42653c951999f3c2ef2f4d933eeecfe1f244369b764d183e937072687fee4a3c22ea28933dda993c3ee72a64a
7
- data.tar.gz: 4d2b967320a263878a2db1d263bdb19cb9cdc440a2b67d72e36f127705ba69218bdab5205b3ec0ee0a9093c6bfea6ca5ffdaf288f636ed7a1ff8e297e3df3c0f
6
+ metadata.gz: 7eb740ca2acb363b8b876f3e5e70bb815f4cbcd417a76b7e61c1190807fd19cbacb1b3f9b9671d0163f3898aa6d351346b86de0087c8093e7a4a8d83e4326d2b
7
+ data.tar.gz: dee61360f7deac2c128747a71584ef290edf417cc9e32adb8a83cec2e341cff77ba4003a819736004acc4a6e0eb29fdac04e1fa6334b137a0437f2c1c5806027
data/README.md CHANGED
@@ -1,10 +1,12 @@
1
1
  # Numo::Gnuplot : Gnuplot interface for Ruby
2
2
 
3
3
  * Alpha version under development.
4
- * [GitHub site](https://github.com/masa16/numo-gnuplot)
4
+ * [GitHub site](https://github.com/ruby-numo/gnuplot)
5
+ * [RubyGems site](https://rubygems.org/gems/numo-gnuplot)
6
+ * [API doc](http://www.rubydoc.info/gems/numo-gnuplot/Numo/Gnuplot)
5
7
 
6
- Although there are many [other Gnuplot interface libraries for Ruby](https://github.com/masa16/numo-gnuplot#related-work),
7
- none of them have so simple interface as to obtain an XY data plot by just typing:
8
+ Although there are many [other Gnuplot interface libraries for Ruby](https://github.com/ruby-numo/gnuplot#related-work),
9
+ none of them have so simple interface as to show an XY data plot by just typing:
8
10
 
9
11
  plot x,y
10
12
 
@@ -44,16 +46,16 @@ require "numo/gnuplot"
44
46
 
45
47
  ```ruby
46
48
  gp = Numo::Gnuplot.new
47
- gp.set title:"First Example"
48
- gp.plot "sin(x)"
49
+ gp.set title:"Example Plot"
50
+ gp.plot "sin(x)",w:"lines"
49
51
  ```
50
52
 
51
- * You can avoid receiver.
53
+ * You can omit receiver.
52
54
 
53
55
  ```ruby
54
56
  Numo::Gnuplot.new.instance_eval do
55
- set title:"Second Example"
56
- plot "sin(x)"
57
+ set title:"Example Plot"
58
+ plot "sin(x)",w:"lines"
57
59
  end
58
60
  ```
59
61
 
@@ -61,18 +63,25 @@ end
61
63
 
62
64
  ```ruby
63
65
  Numo.gnuplot do
64
- set title:"Third Example"
65
- plot "sin(x)"
66
+ set title:"Example Plot"
67
+ plot "sin(x)",w:"lines"
66
68
  end
67
69
  ```
68
70
 
71
+ * In these examples, the following command lines are send to Gnuplot.
72
+
73
+ ```
74
+ set title "Example Plot"
75
+ plot sin(x) w lines
76
+ ```
77
+
69
78
  * Interactive plotting with IRB:
70
79
 
71
80
  ```
72
81
  $ irb -r numo/gnuplot
73
82
  irb(main):001:0> pushb Numo.gnuplot
74
- irb(gnuplot):002:0> set t:"Forth Example"
75
- irb(gnuplot):003:0> plot "sin(x)"
83
+ irb(gnuplot):002:0> set t:"Example Plot"
84
+ irb(gnuplot):003:0> plot "sin(x)",w:"lines"
76
85
  ```
77
86
 
78
87
  * Plotting X-Y data.
@@ -116,9 +125,9 @@ x = Numo::DFloat[-n..n]/n*10
116
125
 
117
126
  Numo.gnuplot do
118
127
  set title:"multiple data series"
119
- # separate by Hash
128
+ # place next data after option Hash
120
129
  plot x,NM.sin(x), {w:'points',t:'sin(x)'}, x,x*NM.sin(x),{w:"lines",t:'x*sin(x)'}
121
- # or separate into Array
130
+ # or place data and options in Array
122
131
  # plot [x,NM.sin(x), w:'points',t:'sin(x)'], [x,x*NM.sin(x),w:"lines",t:'x*sin(x)']
123
132
  # (here last item in each Array should be Hash in order to distinguish from array data)
124
133
  gets
@@ -144,6 +153,42 @@ Numo.gnuplot do
144
153
  end
145
154
  ```
146
155
 
156
+ ## Gnuplot methods
157
+
158
+ Numo::Gnuplot class methods succeeded from Gnuplot commands:
159
+
160
+ * clear
161
+ * exit
162
+ * help(topic)
163
+ * load(filename)
164
+ * pause(*args)
165
+ * plot(*args)
166
+ * quit
167
+ * reflesh
168
+ * replot
169
+ * reset(option)
170
+ * set(*options)
171
+ * show(option)
172
+ * splot(*args)
173
+ * unset(*options)
174
+
175
+ Numo::Gnuplot class methods renamed from Gnuplot commands:
176
+
177
+ * raise_plot(plot_window) -- 'raise' command
178
+ * lower_plot(plot_window) -- 'lower' command
179
+
180
+ Numo::Gnuplot-specific methods:
181
+
182
+ * debug_off -- turn off debug print
183
+ * debug_on -- turn on debug print
184
+ * output(filename,*opts) -- output current plot to file. This invokes the next commands;
185
+ ```ruby
186
+ set terminal:[ext,*opts], output:filename; refresh
187
+ ```
188
+ * var(name) -- returns variable content in the Gnuplot context.
189
+
190
+ See [API doc](http://www.rubydoc.info/gems/numo-gnuplot/Numo/Gnuplot) for more.
191
+
147
192
  ## Related Work
148
193
 
149
194
  * [Ruby Gnuplot](https://github.com/rdp/ruby_gnuplot/tree/master)
@@ -154,4 +199,4 @@ end
154
199
  ## Contributing
155
200
 
156
201
  Bug reports and pull requests are welcome on GitHub at
157
- https://github.com/[USERNAME]/numo-gnuplot.
202
+ https://github.com/ruby-numo/gnuplot.
data/examples/all.rb CHANGED
@@ -1,17 +1,22 @@
1
1
  require "numo/gnuplot"
2
+ #require_relative "../lib/numo/gnuplot"
2
3
 
3
4
  gp = Numo.gnuplot
5
+ #gp.debug_on
4
6
 
5
- puts "mouse/key on window to continue"
6
- Dir.glob("ex*.rb") do |frb|
7
+ puts "enter key to continue"
8
+
9
+ files = Dir.glob("ex*.rb").sort
10
+ files.each do |frb|
7
11
  gp.reset
12
+ puts "*** "+frb+" ***"
8
13
  load frb
9
- gp.pause mouse:"any"
14
+ gets
10
15
  end
11
16
 
12
17
  gp.set term:"png"
13
18
 
14
- Dir.glob("ex*.rb") do |frb|
19
+ files.each do |frb|
15
20
  fimg = File.basename(frb,".rb")+".png"
16
21
  gp.reset
17
22
  gp.set output:fimg
data/examples/ex006.rb ADDED
@@ -0,0 +1,15 @@
1
+ # X-Y data plot from file
2
+ fn = "tmp.dat"
3
+ open(fn,"w") do |f|
4
+ 100.times do |i|
5
+ x = i*0.1
6
+ f.printf("%g %g\n", x, Math.sin(x))
7
+ end
8
+ end
9
+
10
+ Numo.gnuplot do
11
+ set title:"X-Y data plot from file"
12
+ # if 'using' option is given,
13
+ # the first string argument is regarded as a data file.
14
+ plot fn, using:[1,2], w:'lines', t:'sin(x)'
15
+ end
data/examples/ex010.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require "numo/narray"
2
2
 
3
3
  # 3D plot of 2D data with Numo::NArray
4
- n = 60
4
+ n = 120
5
5
  x = (Numo::DFloat.new(1,n).seq/n-0.5)*30
6
6
  y = (Numo::DFloat.new(n,1).seq/n-0.5)*30
7
7
  r = Numo::NMath.sqrt(x**2+y**2) + 1e-10
@@ -9,6 +9,5 @@ z = Numo::NMath.sin(r)/r
9
9
 
10
10
  Numo.gnuplot do
11
11
  set title:'3D plot of 2D data'
12
- set dgrid3d:[60,60]
13
12
  splot z, w:'pm3d', t:'sin(r)/r'
14
13
  end
data/lib/numo/gnuplot.rb CHANGED
@@ -11,7 +11,7 @@ module Numo
11
11
 
12
12
  class Gnuplot
13
13
 
14
- VERSION = "0.1.1"
14
+ VERSION = "0.1.2"
15
15
  POOL = []
16
16
  DATA_FORMAT = "%.5g"
17
17
 
@@ -23,13 +23,17 @@ class Gnuplot
23
23
 
24
24
  def initialize(gnuplot_command="gnuplot")
25
25
  @history = []
26
+ @debug = false
27
+ unless system("which "+gnuplot_command)
28
+ kernel_raise GnuplotError,"Gnuplot command not found"
29
+ end
26
30
  @iow = IO.popen(gnuplot_command+" 2>&1","w+")
27
31
  @ior = @iow
28
32
  @gnuplot_version = send_cmd("print GPVAL_VERSION")[0].chomp
29
- @debug = false
30
33
  end
31
34
 
32
35
  attr_reader :history
36
+ attr_reader :last_message
33
37
  attr_reader :gnuplot_version
34
38
 
35
39
  # draw 2D functions and data.
@@ -45,11 +49,11 @@ class Gnuplot
45
49
  end
46
50
 
47
51
  def _plot_splot(cmd,args)
48
- contents = parse_plot_args(args)
52
+ contents = parse_plot_args(cmd,args)
49
53
  r = contents.shift.map{|x|"[#{x.begin}:#{x.end}] "}.join
50
54
  c = contents.map{|x| x.cmd_str}.join(",")
51
55
  d = contents.map{|x| x.data_str}.join
52
- run "#{cmd} #{r}#{c}\n#{d}"
56
+ run "#{cmd} #{r}#{c}", d
53
57
  nil
54
58
  end
55
59
  private :_plot_splot
@@ -225,8 +229,8 @@ class Gnuplot
225
229
  "gnuplot"
226
230
  end
227
231
 
228
- def run(s)
229
- res = send_cmd(s)
232
+ def run(s,data=nil)
233
+ res = send_cmd(s,data)
230
234
  if !res.empty?
231
235
  if res.size > 7
232
236
  msg = "\n"+res[0..5].join("")+" :\n"
@@ -239,27 +243,28 @@ class Gnuplot
239
243
  end
240
244
  private :run
241
245
 
242
- def send_cmd(s)
246
+ def send_cmd(s,data=nil)
243
247
  puts "<"+s if @debug
244
248
  @iow.puts s
249
+ @iow.puts data
245
250
  @iow.flush
246
251
  @iow.puts "print '_end_of_cmd_'"
247
252
  @iow.flush
248
253
  @history << s
249
- res = []
254
+ @last_message = []
250
255
  while line=@ior.gets
251
256
  puts ">"+line if @debug
252
257
  break if /^_end_of_cmd_$/ =~ line
253
- res << line
258
+ @last_message << line
254
259
  end
255
- res # = res.chomp.strip
260
+ @last_message
256
261
  end
257
262
  private :send_cmd
258
263
 
259
- def parse_plot_args(args)
260
- list = [[]]
261
- item = PlotItem.new
262
- list << item
264
+ def parse_plot_args(cmd,args)
265
+ cPlotItem = (cmd == "plot") ? PlotItem : SPlotItem
266
+ list = [[],cPlotItem.new] # first item is range
267
+ item = list.last
263
268
  args.each do |arg|
264
269
  case arg
265
270
  when Range
@@ -270,23 +275,17 @@ class Gnuplot
270
275
  elsif PlotItem.is_data(arg)
271
276
  item << arg
272
277
  else
273
- if list.last.empty?
274
- list.pop
275
- end
276
- item = PlotItem.new(*arg)
277
- list << item
278
+ list.pop if list.last.empty?
279
+ list << item = cPlotItem.new(*arg) # next PlotItem
278
280
  end
279
281
  when Hash
280
282
  item << arg
281
- item = PlotItem.new
282
- list << item
283
+ list << item = cPlotItem.new # next PlotItem
283
284
  else
284
285
  item << arg
285
286
  end
286
287
  end
287
- if list.last.empty?
288
- list.pop
289
- end
288
+ list.pop if list.last.empty?
290
289
  return list
291
290
  end
292
291
  private :parse_plot_args
@@ -383,7 +382,11 @@ class Gnuplot
383
382
  when FalseClass
384
383
  nil
385
384
  when Array
386
- "#{k} #{OptsToS.new(*v)}"
385
+ if /^#{k}/ =~ "using"
386
+ "#{k} #{v.join(':')}"
387
+ else
388
+ "#{k} #{OptsToS.new(*v)}"
389
+ end
387
390
  else
388
391
  "#{k} #{OptsToS.new(v)}"
389
392
  end
@@ -406,10 +409,10 @@ class Gnuplot
406
409
  end
407
410
  elsif defined?(Numo::NArray)
408
411
  return true if a.kind_of?(Numo::NArray)
409
- elsif defined?(NArray)
410
- return true if a.kind_of?(NArray)
411
- elsif defined?(NMatrix)
412
- return true if a.kind_of?(NMatix)
412
+ elsif defined?(::NArray)
413
+ return true if a.kind_of?(::NArray)
414
+ elsif defined?(::NMatrix)
415
+ return true if a.kind_of?(::NMatix)
413
416
  end
414
417
  false
415
418
  end
@@ -433,38 +436,37 @@ class Gnuplot
433
436
  elsif @items.first.kind_of? String
434
437
  @function = @items.first
435
438
  @options = @items[1..-1]
439
+ if (o=@items.last).kind_of? Hash
440
+ if o.any?{|k,v| /^#{k}/ =~ "using"}
441
+ # @function is data file
442
+ @function = "'#{@function}'"
443
+ end
444
+ end
436
445
  else
437
- @data = []
446
+ data = []
438
447
  @options = []
439
448
  @items.each do |x|
440
449
  if PlotItem.is_data(x)
441
- @data << x
450
+ data << x
442
451
  else
443
452
  @options << x
444
453
  end
445
454
  end
446
- if @data.size==1
447
- a = @data[0]
448
- if a.respond_to?(:shape)
449
- if a.shape.size == 2
450
- @matrix = true
451
- end
452
- end
453
- end
455
+ @data = parse_data(data)
454
456
  end
455
457
  end
456
458
  end
457
459
 
460
+ def parse_data(data)
461
+ PlotData.new(*data)
462
+ end
463
+
458
464
  def cmd_str
459
465
  parse_items
460
466
  if @function
461
467
  "%s %s" % [@function, OptsToS.new(*@options)]
462
468
  else
463
- if @matrix
464
- "'-' matrix %s" % OptsToS.new(*@options)
465
- else
466
- "'-' %s" % OptsToS.new(*@options)
467
- end
469
+ "%s %s" % [@data.cmd_str, OptsToS.new(*@options)]
468
470
  end
469
471
  end
470
472
 
@@ -473,38 +475,114 @@ class Gnuplot
473
475
  if @function
474
476
  nil
475
477
  else
476
- if @matrix
477
- data2d_to_s(@data[0])+"\ne\n"
478
+ @data.data_str
479
+ end
480
+ end
481
+
482
+ end # PlotItem
483
+
484
+
485
+ # @private
486
+ class SPlotItem < PlotItem # :nodoc: all
487
+ def parse_data(data)
488
+ if data.size == 1
489
+ if data[0].respond_to?(:shape)
490
+ SPlotArray.new(*data)
478
491
  else
479
- data1d_to_s(@data)+"e\n"
492
+ PlotData.new(*data)
480
493
  end
494
+ else
495
+ SPlotRecord.new(*data)
481
496
  end
482
497
  end
498
+ end
499
+
483
500
 
501
+ # @private
502
+ class PlotData # :nodoc: all
484
503
  def data_format
485
504
  @data_format || DATA_FORMAT
486
505
  end
487
506
 
488
- def data_format=(s)
489
- @data_format = s
507
+ def initialize(*data)
508
+ @data = data.map{|a| a.flatten}
509
+ @n = @data.map{|a| a.size}.min
510
+ @text = false
490
511
  end
491
512
 
492
- def data1d_to_s(a)
493
- n = a.map{|e| e.size}.min
494
- f = ([data_format]*a.size).join(" ")+"\n"
495
- s = ""
496
- n.times{|i| s << f % a.map{|e| e[i]}}
497
- s
513
+ def cmd_str
514
+ if @text
515
+ "'-'"
516
+ else
517
+ "'-' binary record=#{@n} format='%float64'"
518
+ end
498
519
  end
499
520
 
500
- def data2d_to_s(a)
501
- f = data_format
502
- s = ""
503
- a.to_a.each do |b|
504
- s << b.map{|e| f%e}.join(" ")+"\n"
521
+ def data_str
522
+ if @text
523
+ f = ([data_format]*@data.size).join(" ")+"\n"
524
+ s = ""
525
+ @n.times{|i| s << f % @data.map{|a| a[i]}}
526
+ s+"\ne"
527
+ elsif defined? Numo::NArray
528
+ m = @data.size
529
+ x = Numo::DFloat.zeros(@n,m)
530
+ m.times{|i| x[true,i] = @data[i][0...@n]}
531
+ x.to_string
532
+ else
533
+ s = []
534
+ @n.times{|i| s.concat @data.map{|a| a[i]}}
535
+ s.pack("d*")
505
536
  end
506
- s
507
537
  end
508
- end # PlotItem
538
+ end
539
+
540
+ # @private
541
+ class SPlotRecord < PlotData # :nodoc: all
542
+ def initialize(*data)
543
+ @shape = data.last.shape
544
+ super
545
+ end
546
+
547
+ def cmd_str
548
+ if @text
549
+ "'-'"
550
+ else
551
+ "'-' binary record=(#{@shape[1]},#{@shape[0]}) format='%float64' using 1:2:3"
552
+ end
553
+ end
554
+ end
555
+
556
+ # @private
557
+ class SPlotArray < PlotData # :nodoc: all
558
+ def initialize(data)
559
+ @data = data
560
+ end
561
+
562
+ def cmd_str
563
+ if @text
564
+ "'-' matrix"
565
+ else
566
+ s = @data.shape
567
+ "'-' binary array=(#{s[1]},#{s[0]}) format='%float64'"
568
+ end
569
+ end
570
+
571
+ def data_str
572
+ if @text
573
+ f = data_format
574
+ s = ""
575
+ a.each do |b|
576
+ s << b.map{|e| f%e}.join(" ")+"\n"
577
+ end
578
+ s+"\ne"
579
+ elsif defined? Numo::NArray
580
+ Numo::DFloat.cast(@data).to_string
581
+ else
582
+ @data.to_a.flatten.pack("d*")
583
+ end
584
+ end
585
+ end
586
+
509
587
  end # Numo::Gnuplot
510
588
  end
data/numo-gnuplot.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
 
12
12
  spec.summary = %q{Gnuplot interface for Ruby.}
13
13
  spec.description = %q{Gnuplot interface for Ruby with simple and similar inteface with Gnuplot.}
14
- spec.homepage = "https://github.com/masa16/numo-gnuplot"
14
+ spec.homepage = "https://github.com/ruby-numo/gnuplot"
15
15
 
16
16
  # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
17
  # delete this section to allow pushing this gem to any host.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: numo-gnuplot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masahiro TANAKA
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-23 00:00:00.000000000 Z
11
+ date: 2016-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -56,12 +56,13 @@ files:
56
56
  - examples/ex003.rb
57
57
  - examples/ex004.rb
58
58
  - examples/ex005.rb
59
+ - examples/ex006.rb
59
60
  - examples/ex010.rb
60
61
  - examples/ex011.rb
61
62
  - lib/numo/gnuplot.rb
62
63
  - numo-gnuplot.gemspec
63
64
  - setup.rb
64
- homepage: https://github.com/masa16/numo-gnuplot
65
+ homepage: https://github.com/ruby-numo/gnuplot
65
66
  licenses: []
66
67
  metadata: {}
67
68
  post_install_message: