Class: Digiproc::Rbplot::LinePlot
Overview
Class for a line plot
Instance Method Summary collapse
-
#add_line(x, y, label = "data #{@dataxy.length + 1}") ⇒ Object
Add another line to the plot y2 = Digiproc::Probability.nrand(100) plt.add_line(x, y2, “Data 2”).
-
#data_label(label) ⇒ Object
Sets data label for the last inputted line data plt.data_label(‘Set 3’).
-
#filename(name) ⇒ Object
sets filename for the image to be written downcases, strips, and replaces spaces with dashes.
-
#initialize(x, y, label = "data 1") ⇒ LinePlot
constructor
A new instance of LinePlot.
-
#legend(*labels) ⇒ Object
Sets the labels for each line entered (variable array input) plt.legend(“Data 1”, “Data 2”).
-
#path(path) ⇒ Object
Sets the path where the image will be written Defaults to “./” plt.path(“./”).
-
#show(path = @path) ⇒ Object
Writes the image and opens it with the default program depending on the os plt.show.
-
#size(w, h) ⇒ Object
Set size of the graph in pixels.
-
#theme(theme) ⇒ Object
Sets the theme of the graph Accepts :dark, :light, or :deep plt.theme(:dark).
-
#title(title) ⇒ Object
Sets title for the graph plt.title(‘Plot Title’).
-
#write(path = @path) ⇒ Object
Writes the image to the saved path, does not open it plt.write.
-
#xlabel(label) ⇒ Object
Sets the x label: plt.xlabel(‘time’).
-
#xsteps(steps) ⇒ Object
Sets the number of labels on the x axis plt.xsteps(5).
-
#ylabel(label) ⇒ Object
Sets the y label plt.ylabel(‘y axis’).
Methods included from OS
#linux?, #mac?, #ruby?, #unix?, #windows?
Constructor Details
#initialize(x, y, label = "data 1") ⇒ LinePlot
Returns a new instance of LinePlot.
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rbplot.rb', line 64 def initialize(x, y, label = "data 1") @methods = { line_width: 2.5, dot_radius: 0.1, theme: BLUESCALE, title: 'RbPlot' } @dataxy = [[label, x, y]] @path = './' @filename = nil @size = '1000x1000' self.xsteps(5) end |
Instance Method Details
#add_line(x, y, label = "data #{@dataxy.length + 1}") ⇒ Object
Add another line to the plot y2 = Digiproc::Probability.nrand(100) plt.add_line(x, y2, “Data 2”)
126 127 128 |
# File 'lib/rbplot.rb', line 126 def add_line(x, y, label="data #{@dataxy.length + 1}") @dataxy << [label, x, y] end |
#data_label(label) ⇒ Object
Sets data label for the last inputted line data plt.data_label(‘Set 3’)
109 110 111 |
# File 'lib/rbplot.rb', line 109 def data_label(label) @dataxy.last[0] = label end |
#filename(name) ⇒ Object
sets filename for the image to be written downcases, strips, and replaces spaces with dashes
81 82 83 |
# File 'lib/rbplot.rb', line 81 def filename(name) @filename = name.downcase.strip.gsub(' ', '-') end |
#legend(*labels) ⇒ Object
Sets the labels for each line entered (variable array input) plt.legend(“Data 1”, “Data 2”)
116 117 118 119 120 |
# File 'lib/rbplot.rb', line 116 def legend(*labels) labels.each.with_index do |l, i| @dataxy[i][0] = l end end |
#path(path) ⇒ Object
Sets the path where the image will be written Defaults to “./” plt.path(“./”)
151 152 153 |
# File 'lib/rbplot.rb', line 151 def path(path) @path = path end |
#show(path = @path) ⇒ Object
Writes the image and opens it with the default program depending on the os plt.show
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/rbplot.rb', line 182 def show(path = @path) write(path) file = path + @filename + '.png' if windows? system %{cmd /c "start #{file}"} elsif mac? file_to_open = "/path/to/file.txt" system %{open "#{file}"} elsif linux? begin system %{xdg-open "#{file}"} system %{cmd.exe /c "start #{file}"} rescue system %{cmd.exe /c "start #{file}"} end end end |
#size(w, h) ⇒ Object
Set size of the graph in pixels. Takes two integers plt.size(2000, 2000). Defaults upon initialization to 1000,1000
175 176 177 |
# File 'lib/rbplot.rb', line 175 def size(w,h) @size = "#{w}x#{h}" end |
#theme(theme) ⇒ Object
Sets the theme of the graph Accepts :dark, :light, or :deep plt.theme(:dark)
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/rbplot.rb', line 159 def theme(theme) case theme when :dark @methods[:theme] = MIDNIGHT when :deep @methods[:theme] = SUBMARINE when :light @methods[:theme] = BLUESCALE else throw ArgumentError.new('Not a valid theme') end end |
#title(title) ⇒ Object
Sets title for the graph plt.title(‘Plot Title’)
87 88 89 90 |
# File 'lib/rbplot.rb', line 87 def title(title) filename(title) @methods[:title] = title end |
#write(path = @path) ⇒ Object
Writes the image to the saved path, does not open it plt.write
203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/rbplot.rb', line 203 def write(path = @path) gline = Gruff::Line.new(@size) @methods.each do |m, args| gline.send("#{m}=", args) end @dataxy.each do |dxy| gline.dataxy(dxy[0], dxy[1], dxy[2]) end @filename ||= filename(@methods[:title]) @filename ||= "rbPlot" gline.write(path + @filename + '.png') end |
#xlabel(label) ⇒ Object
Sets the x label: plt.xlabel(‘time’)
95 96 97 |
# File 'lib/rbplot.rb', line 95 def xlabel(label) @methods[:x_axis_label] = label end |
#xsteps(steps) ⇒ Object
Sets the number of labels on the x axis plt.xsteps(5)
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/rbplot.rb', line 133 def xsteps(steps) len = @dataxy.first[1].length steps = len if(steps >= len) labels = {} every = (len.to_f / steps).floor for i in 0..steps do index = i == 0 ? 0 : (i * every) - 1 labels_val = i == 0 ? 1 : i * every # labels[labels_val] = @dataxy.first[1][index].round(2) labels[@dataxy.first[1][index]] = @dataxy.first[1][index].round(2) end @methods[:labels] = labels end |
#ylabel(label) ⇒ Object
Sets the y label plt.ylabel(‘y axis’)
102 103 104 |
# File 'lib/rbplot.rb', line 102 def ylabel(label) @methods[:y_axis_label] = label end |