Class: SimplePlot

Inherits:
SimpleOutput::SimpleOutputPlugin show all
Defined in:
lib/simpleplot.rb

Overview

SimplePlot

GnuPlot interface to simpleoutput

 Copyright 2014 Austen Higgins-Cassidy

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

Instance Method Summary collapse

Methods inherited from SimpleOutput::SimpleOutputPlugin

#advance_series, #annotate, #appendHash, #appendPoints, #appendXY, #append_series_name, #getDataAsPoints, #getDataAsXY, #getSeriesHashes, #newData, #setHash, #setOptions, #setPoints, #setXData, #setXY, #setYData, #translate_name

Constructor Details

#initialize(name_template = "_plot", size = 512) ⇒ SimplePlot

Returns a new instance of SimplePlot.



23
24
25
26
27
28
29
# File 'lib/simpleplot.rb', line 23

def initialize(name_template="_plot", size = 512)
   super()
   @name = name_template
   @size = size
   @series_next = 0;
    = {}
end

Instance Method Details

#append_callback(x, y, name, options) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/simpleplot.rb', line 91

def append_callback(x,y,name,options)
  xmin = x.min
  xmax = x.max
  ymin = y.min
  ymax = y.max
  if !.has_key?(name)
    new_data_callback(name)
  end
  [name]['length'] = [name]['length'] < x.size ? x.size : [name]['length']
  [name]['xmin'] = xmin < [name]['xmin'] ? xmin : [name]['xmin']
  [name]['xmax'] = xmax > [name]['xmax'] ? xmax : [name]['xmax']
  [name]['ymin'] = ymin < [name]['ymin'] ? ymin : [name]['ymin']
  [name]['ymax'] = ymax > [name]['ymax'] ? ymax : [name]['ymax']
  check_title(name, options)
  
end

#check_title(name, options) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/simpleplot.rb', line 61

def check_title(name, options)
  if options.has_key?('series')
    [name]['series_titles'] << options['series']
  else
    [name]['series_titles'] << "set-#{@series_next}"
    @series_next += 1
  end
end

#new_data_callback(name) ⇒ Object



70
71
72
73
# File 'lib/simpleplot.rb', line 70

def new_data_callback(name)
  name = translate_name(name)
  [name] = {'length' => 0, 'xlabel' => 'x', 'ylabel' => 'y', 'xmin' => 0 , 'xmax' => 10, 'ymin' => 0, 'ymax' => 10, 'series_titles' => [], 'histogram' => false, 'bincount' => 10, 'normalized' => false}
end

#options_callback(options) ⇒ Object



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
# File 'lib/simpleplot.rb', line 31

def options_callback(options)
  if options.has_key?('xlabel')
    [@current_name]['xlabel'] = options['xlabel']
  end
  if options.has_key?('ylabel')
    [@current_name]['ylabel'] = options['ylabel']
  end
  if options.has_key?('histogram')
    [@current_name]['histogram'] = options['histogram']
  end
  if options.has_key?('xmin')
    [@current_name]['xmin'] = options['xmin']
  end
  if options.has_key?('xmax')
    [@current_name]['xmax'] = options['xmax']
  end
  if options.has_key?('ymin')
    [@current_name]['ymin'] = options['ymin']
  end
  if options.has_key?('ymax')
    [@current_name]['ymax'] = options['ymax']
  end
  if options.has_key?('bincount')
    [@current_name]['bincount'] = options['binscount']
  end
  if options.has_key?('normalized')
    [@current_name]['normalized'] = options['normalized']
  end
end

#saveObject



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
163
164
165
166
# File 'lib/simpleplot.rb', line 108

def save()
  data = self.getDataAsXY()
  data.each do |set_name, series|
    Gnuplot.open do |gp|
      Gnuplot::Plot.new(gp) do |plot|
        plot.terminal "png"
        #plot.size 0.95

       
        plot.output "#{set_name+@name}.png"

        plot.title set_name

        plot.xlabel [set_name]['xlabel']
        plot.ylabel [set_name]['ylabel']
        plot.data = []
       
        if [set_name]['histogram']
          size = [set_name]['length']
          bins = [set_name]['bincount']
          max = [set_name]['ymax']
          min = [set_name]['ymin']
         
          width = (max.to_f-min.to_f).to_f/bins.to_f
          #bins = size.to_f/width.to_f

          plot.yrange '[0:]'
          plot.xrange "[#{min}:#{max}]"
          plot.set('boxwidth',width*0.9)
          plot.set('offset', 'graph 0.05,0.05,0.05,0.0')
          plot.set('xtics' " #{min}, #{width.to_f}, #{max}")
          plot.set('tics', 'out nomirror')
          plot.set('style', 'fill solid 0.5')
        else
          plot.xrange "[#{@metadata[set_name]['xmin']}:#{@metadata[set_name]['xmax']}]"
          plot.yrange "[#{@metadata[set_name]['ymin']}:#{@metadata[set_name]['ymax']}]"
        end
        series.each_with_index do |line, index|
          
          if [set_name]['histogram']
            data_pts = line[1]
          else
            data_pts = line
          end
           
          d = Gnuplot::DataSet.new(data_pts) 
          d.title = [set_name]['series_titles'][index]
         
          if [set_name]['histogram']
            d.using = "(#{width.to_f}*floor($1/#{width.to_f})+#{width.to_f}/2.0):(1.0) smooth freq w boxes lc rgb\"blue\""
          else
             d.with = "linespoints"
             d.linewidth = 2
          end
          plot.data << d
          
        end
      end
    end
  end
end

#set_x_callback(data, name, options) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/simpleplot.rb', line 75

def set_x_callback(data, name, options)
  xmin = data.min
  xmax = data.max
  [name]['xmin'] = xmin
  [name]['xmax'] = xmax
  [name]['length'] = ([name]['length'] < data.size) ? data.size : [name]['length']
  check_title(name, options)
end

#set_y_callback(data, name, options) ⇒ Object



84
85
86
87
88
89
# File 'lib/simpleplot.rb', line 84

def set_y_callback(data, name, options)
  ymin = data.min
  ymax = data.max
  [name]['ymin'] = ymin
  [name]['ymax'] = ymax
end