class Digiproc::Rbplot::LinePlot
Class for a line plot
Public Class Methods
new(x, y, label = "data 1")
click to toggle source
# 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
Public Instance Methods
add_line(x, y, label="data
click to toggle source
Add another line to the plot y2 = Digiproc::Probability.nrand(100)
plt.add_line(x, y2, "Data 2")
# File lib/rbplot.rb, line 126 def add_line(x, y, label="data #{@dataxy.length + 1}") @dataxy << [label, x, y] end
data_label(label)
click to toggle source
Sets data label for the last inputted line data
plt.data_label('Set 3')
# File lib/rbplot.rb, line 109 def data_label(label) @dataxy.last[0] = label end
filename(name)
click to toggle source
sets filename for the image to be written downcases, strips, and replaces spaces with dashes
# File lib/rbplot.rb, line 81 def filename(name) @filename = name.downcase.strip.gsub(' ', '-') end
legend(*labels)
click to toggle source
Sets the labels for each line entered (variable array input)
plt.legend("Data 1", "Data 2")
# File lib/rbplot.rb, line 116 def legend(*labels) labels.each.with_index do |l, i| @dataxy[i][0] = l end end
path(path)
click to toggle source
Sets the path where the image will be written Defaults to “./”
plt.path("./")
# File lib/rbplot.rb, line 151 def path(path) @path = path end
show(path = @path)
click to toggle source
Writes the image and opens it with the default program depending on the os
plt.show
# 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)
click to toggle source
Set size of the graph in pixels. Takes two integers
plt.size(2000, 2000). Defaults upon initialization to 1000,1000
# File lib/rbplot.rb, line 175 def size(w,h) @size = "#{w}x#{h}" end
theme(theme)
click to toggle source
Sets the theme of the graph Accepts :dark, :light, or :deep
plt.theme(:dark)
# 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)
click to toggle source
Sets title for the graph
plt.title('Plot Title')
# File lib/rbplot.rb, line 87 def title(title) filename(title) @methods[:title] = title end
write(path = @path)
click to toggle source
Writes the image to the saved path, does not open it
plt.write
# 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)
click to toggle source
Sets the x label:
plt.xlabel('time')
# File lib/rbplot.rb, line 95 def xlabel(label) @methods[:x_axis_label] = label end
xsteps(steps)
click to toggle source
Sets the number of labels on the x axis
plt.xsteps(5)
# 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)
click to toggle source
Sets the y label
plt.ylabel('y axis')
# File lib/rbplot.rb, line 102 def ylabel(label) @methods[:y_axis_label] = label end