class Digiproc::QuickPlot

Class for quickly plotting data

Public Class Methods

lambda_plot(label = "1") click to toggle source

Plots a lambda plot

# File lib/quick_plot.rb, line 58
def self.lambda_plot(label = "1")
    eqn = ->(t){ t.abs <= 1 ? (1 - t.abs) : 0}
    x = Digiproc::Functions.linspace(-2, 2, 102)
    plot(data: x.map{|t| eqn.call(t)},title: "Lambda fn" ,labels: {26 => "-#{label}", 51 => 0 ,76 => label}) 
end
pi_plot(label ="0.5") click to toggle source

Plots a pi plot

# File lib/quick_plot.rb, line 47
def self.pi_plot(label ="0.5")
    z = Digiproc::Functions.zeros(26)
    o = Digiproc::Functions.ones(50)
    y = z + o + z
    x = Digiproc::Functions.linspace(-2, 2, 102)
    plot(x: x, y: y, title: "PI fn" ,labels: {-1 => "-#{label}", 0 => "0", 1 => label}) 
end
plot(x: nil,y: nil, data: nil, title: nil, x_label: nil, y_label: nil, labels: nil, data_name: "data", label_map: nil, dark: false, path: "./plots/") click to toggle source

Quickly plot data. Either data OR x AND y are required. If just data is inputted, x values will be the index of the value in the data array

Input Args

x

Array (required if data is not included)

y

Array (required if data is not included)

data

Array (required if x and y are not included)

title (Optional)

String

x_label (Optional)

String

y_label (Optional)

String

labels (Optional)

Array x value labels

data_name (Optional)

name of data for legend

label_map (Optional)

Lambda - mapping data index to an x label for that index

dark (Optional)

Boolean - whether or not to use dark mode (defaults to false)

path (Optional)

Path to save plot picture to. Defauilts to “./plots”

# File lib/quick_plot.rb, line 23
def self.plot(x: nil,y: nil, data: nil, title: nil, x_label: nil, y_label: nil, labels: nil, data_name: "data", label_map: nil, dark: false, path: "./plots/")
    xyname, dataname = nil, nil
    if not x.nil?
        if evenly_spaced?(x)
            label_map = Digiproc::Functions.map_to_eqn(0, x.length, x.min, x.max)
            data = y
            y, x = nil, nil
        end
    end
    data.nil? ? (xyname = data_name) : (dataname = data_name)
    qplot(x: x, y: y, data: data, data_name: dataname, xyname: xyname, label_map: label_map, filename: to_filename(title), path: path) do |g|
        g.title = title if not title.nil?
        g.x_axis_label = x_label if not x_label.nil?
        g.y_axis_label = y_label if not y_label.nil?
        g.labels = labels if not labels.nil?
        g.theme = Digiproc::Plottable::Styles::MIDNIGHT if dark
    end
end
plot_eq(eqn: , sample_times: ,title: nil, x_label: nil, y_label: nil) click to toggle source

Plots an input equation (lambda)

Input args

eqn

lambda, equation to pe plotted

sample_times

Array input values to lambda equation

title (Optional)

String

x_label (Optional)

String

y_label (Optional)

String

# File lib/quick_plot.rb, line 72
def self.plot_eq(eqn: , sample_times: ,title: nil, x_label: nil, y_label: nil)
    data = process(eqn, sample_times)
    plot(data: data, title: title, x_label: x_label, y_label: y_label)
end
step() click to toggle source
# File lib/quick_plot.rb, line 42
def self.step()
end

Private Class Methods

evenly_spaced?(data) click to toggle source
# File lib/quick_plot.rb, line 83
def self.evenly_spaced?(data)
    interval = data[1] - data[0] 
    for i in 2...data.length do
        new_int = data[i] - data[i-1]
        return false if new_int.round(10) != interval.round(10)
    end
    true
end
to_filename(title) click to toggle source
# File lib/quick_plot.rb, line 92
def self.to_filename(title)
    title.nil? ? "Quickplot Graph" : title.downcase.gsub(" ", "_")
end

Public Instance Methods

process(eqn, locations) click to toggle source
# File lib/quick_plot.rb, line 77
def process(eqn, locations)
    locations.map{ |n| eqn.call(n) }
end