17 Commits

Author SHA1 Message Date
kojix2
c73da80de6 v0.2.6 2020-10-12 07:45:42 +09:00
kojix2
2e8641ccea Improved descs 2020-10-11 09:23:57 +09:00
kojix2
1b43f7d48f Add comment about pass 2020-10-11 08:29:02 +09:00
kojix2
d8396fecf9 Rubocop auto correct 2020-10-11 08:28:39 +09:00
kojix2
4660c2ab02 Extract case-branching by fmt into another method 2020-10-11 08:02:43 +09:00
kojix2
d7e49f048f Add a comment to params 2020-10-11 07:55:20 +09:00
kojix2
34ae2b5815 v0.2.5 2020-10-11 00:14:18 +09:00
kojix2
7e8dc6190c Fix arg order 2020-10-11 00:11:54 +09:00
kojix2
ba105ab1f3 Improved help desc 2020-10-10 23:58:53 +09:00
kojix2
96a1d1feb9 Fix style 2020-10-10 23:58:53 +09:00
kojix2
2b65dae60c Add a image to README.md 2020-10-10 23:35:26 +09:00
kojix2
943d4e6c44 Use keyword arg for count 2020-10-10 23:21:55 +09:00
kojix2
de33805c56 Drop support for ruby 2.3 2020-10-10 23:18:27 +09:00
kojix2
de3a366d15 Fix debug option 2020-10-10 23:16:15 +09:00
kojix2
4544c0e456 Removed count option for barplot 2020-10-10 23:08:20 +09:00
kojix2
ccfbaa7bde Allows you to specify the output file 2020-09-29 18:16:42 +09:00
kojix2
3aceae9279 Improved command description 2020-09-29 18:14:41 +09:00
7 changed files with 101 additions and 103 deletions

View File

@@ -26,27 +26,7 @@ ruby -r numo/narray -e "puts Numo::DFloat.new(1000).rand_norm.to_a" \
| uplot hist --nbins 15
```
```
┌ ┐
[-4.5, -4.0) ┤ 1
[-4.0, -3.5) ┤ 0
[-3.5, -3.0) ┤ 1
[-3.0, -2.5) ┤▇▇ 9
[-2.5, -2.0) ┤▇▇▇ 15
[-2.0, -1.5) ┤▇▇▇▇▇▇▇▇▇ 50
[-1.5, -1.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 97
[-1.0, -0.5) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 154
[-0.5, 0.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 193
[ 0.0, 0.5) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 165
[ 0.5, 1.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 152
[ 1.0, 1.5) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 86
[ 1.5, 2.0) ┤▇▇▇▇▇▇▇▇▇ 51
[ 2.0, 2.5) ┤▇▇▇▇ 21
[ 2.5, 3.0) ┤▇ 3
[ 3.0, 3.5) ┤ 2
└ ┘
Frequency
```
<img src="https://i.imgur.com/wpsoGJq.png" width="75%" height="75%"></img>
**scatter**

View File

@@ -25,10 +25,10 @@ module Uplot
delimiter = parser.delimiter
transpose = parser.transpose
headers = parser.headers
pass = parser.pass
output = parser.output
count = parser.count
fmt = parser.fmt
debug = parser.debug
@debug = parser.debug
if command == :colors
Plot.colors
@@ -36,33 +36,47 @@ module Uplot
end
# Sometimes the input file does not end with a newline code.
while input = Kernel.gets(nil)
while (input = Kernel.gets(nil))
input.freeze
@raw_inputs << input
@data = Preprocessing.input(input, delimiter, headers, transpose)
pp @data if @debug
case command
when :bar, :barplot
Plot.barplot(data, params, @count)
when :count, :c
Plot.barplot(data, params, count = true)
when :hist, :histogram
Plot.histogram(data, params)
when :line, :lineplot
Plot.line(data, params)
when :lines, :lineplots
Plot.lines(data, params, fmt)
when :scatter, :s
Plot.scatter(data, params, fmt)
when :density, :d
Plot.density(data, params, fmt)
when :box, :boxplot
Plot.boxplot(data, params)
else
raise "unrecognized plot_type: #{command}"
end.render($stderr)
plot = case command
when :bar, :barplot
Plot.barplot(data, params)
when :count, :c
Plot.barplot(data, params, count: true)
when :hist, :histogram
Plot.histogram(data, params)
when :line, :lineplot
Plot.line(data, params)
when :lines, :lineplots
Plot.lines(data, params, fmt)
when :scatter, :s
Plot.scatter(data, params, fmt)
when :density, :d
Plot.density(data, params, fmt)
when :box, :boxplot
Plot.boxplot(data, params)
else
raise "unrecognized plot_type: #{command}"
end
print input if output
if output.is_a?(IO)
plot.render(output)
else
File.open(output, 'w') do |f|
plot.render(f)
end
end
if pass.is_a?(IO)
print input
elsif pass
File.open(pass, 'w') do |f|
f.print(input)
end
end
end
end
end

View File

@@ -2,6 +2,11 @@
module Uplot
class Command
# UnicodePlot parameters.
# * Normally in a Ruby program, you might use hash for the parameter object.
# * Here, I use Struct for 2 safety reason.
# * The keys are static in Struct.
# * Struct does not conflict with keyword arguments. Hash dose.
Params = Struct.new(
# Sort me!
:title,

View File

@@ -7,7 +7,7 @@ module Uplot
class Command
class Parser
attr_reader :command, :params,
:delimiter, :transpose, :headers, :output, :count, :fmt, :debug
:delimiter, :transpose, :headers, :pass, :output, :fmt, :debug
def initialize
@command = nil
@@ -16,8 +16,8 @@ module Uplot
@delimiter = "\t"
@transpose = false
@headers = nil
@output = false
@count = false
@pass = false
@output = $stderr
@fmt = 'xyy'
@debug = false
end
@@ -26,49 +26,53 @@ module Uplot
OptionParser.new do |opt|
opt.program_name = 'uplot'
opt.version = Uplot::VERSION
opt.on('-O', '--output', TrueClass) do |v|
opt.on('-O', '--pass [VAL]', 'file to output standard input data to [stdout]',
'for inserting uplot in the middle of Unix pipes') do |v|
@pass = v || $stdout
end
opt.on('-o', '--output VAL', 'file to output results to [stderr]') do |v|
@output = v
end
opt.on('-d', '--delimiter VAL', 'use DELIM instead of TAB for field delimiter', String) do |v|
opt.on('-d', '--delimiter VAL', String, 'use DELIM instead of TAB for field delimiter') do |v|
@delimiter = v
end
opt.on('-H', '--headers', 'specify that the input has header row', TrueClass) do |v|
opt.on('-H', '--headers', TrueClass, 'specify that the input has header row') do |v|
@headers = v
end
opt.on('-T', '--transpose', TrueClass) do |v|
@transpose = v
end
opt.on('-t', '--title VAL', 'print string on the top of plot', String) do |v|
opt.on('-t', '--title VAL', String, 'print string on the top of plot') do |v|
params.title = v
end
opt.on('-x', '--xlabel VAL', 'print string on the bottom of the plot', String) do |v|
opt.on('-x', '--xlabel VAL', String, 'print string on the bottom of the plot') do |v|
params.xlabel = v
end
opt.on('-y', '--ylabel VAL', 'print string on the far left of the plot', String) do |v|
opt.on('-y', '--ylabel VAL', String, 'print string on the far left of the plot') do |v|
params.ylabel = v
end
opt.on('-w', '--width VAL', 'number of characters per row', Integer) do |v|
opt.on('-w', '--width VAL', Integer, 'number of characters per row') do |v|
params.width = v
end
opt.on('-h', '--height VAL', 'number of rows', Numeric) do |v|
opt.on('-h', '--height VAL', Numeric, 'number of rows') do |v|
params.height = v
end
opt.on('-b', '--border VAL', 'specify the style of the bounding box', String) do |v|
opt.on('-b', '--border VAL', String, 'specify the style of the bounding box') do |v|
params.border = v.to_sym
end
opt.on('-m', '--margin VAL', 'number of spaces to the left of the plot', Numeric) do |v|
opt.on('-m', '--margin VAL', Numeric, 'number of spaces to the left of the plot') do |v|
params.margin = v
end
opt.on('-p', '--padding VAL', 'space of the left and right of the plot', Numeric) do |v|
opt.on('-p', '--padding VAL', Numeric, 'space of the left and right of the plot') do |v|
params.padding = v
end
opt.on('-c', '--color VAL', 'color of the drawing', String) do |v|
opt.on('-c', '--color VAL', String, 'color of the drawing') do |v|
params.color = v =~ /\A[0-9]+\z/ ? v.to_i : v.to_sym
end
opt.on('--[no-]labels', 'hide the labels', TrueClass) do |v|
opt.on('--[no-]labels', TrueClass, 'hide the labels') do |v|
params.labels = v
end
opt.on('--fmt VAL', 'xyy, xyxy', String) do |v|
opt.on('--fmt VAL', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...', 'xyy : header is like x, y1, y2, y2, y3...') do |v|
@fmt = v
end
opt.on('--debug', TrueClass) do |v|
@@ -84,7 +88,9 @@ module Uplot
main_parser.banner = \
<<~MSG
Program: uplot (Tools for plotting on the terminal)
Version: #{Uplot::VERSION} (using unicode_plot #{UnicodePlot::VERSION})
Version: #{Uplot::VERSION} (using UnicodePlot #{UnicodePlot::VERSION})
Author: kojix2 <2xijok@gmail.com>
Source: https://github.com/kojix2/uplot
Usage: uplot <command> [options]
@@ -115,40 +121,37 @@ module Uplot
exit 1
when :barplot, :bar
parser.on('--symbol VAL', String) do |v|
parser.on('--symbol VAL', String, 'character to be used to plot the bars') do |v|
params.symbol = v
end
parser.on('--xscale VAL', String) do |v|
parser.on('--xscale VAL', String, 'axis scaling') do |v|
params.xscale = v
end
parser.on('--count', TrueClass) do |v|
@count = v
end
when :count, :c
parser.on('--symbol VAL', String) do |v|
parser.on('--symbol VAL', String, 'character to be used to plot the bars') do |v|
params.symbol = v
end
when :histogram, :hist
parser.on('-n', '--nbins VAL', Numeric) do |v|
parser.on('-n', '--nbins VAL', Numeric, 'approximate number of bins') do |v|
params.nbins = v
end
parser.on('--closed VAL', String) do |v|
params.closed = v
end
parser.on('--symbol VAL', String) do |v|
parser.on('--symbol VAL', String, 'character to be used to plot the bars') do |v|
params.symbol = v
end
when :lineplot, :line
parser.on('--canvas VAL', String) do |v|
parser.on('--canvas VAL', String, 'type of canvas') do |v|
params.canvas = v
end
parser.on('--xlim VAL', Array) do |v|
parser.on('--xlim VAL', Array, 'plotting range for the x coordinate') do |v|
params.xlim = v.take(2)
end
parser.on('--ylim VAL', Array) do |v|
parser.on('--ylim VAL', Array, 'plotting range for the y coordinate') do |v|
params.ylim = v.take(2)
end
@@ -156,10 +159,10 @@ module Uplot
parser.on('--canvas VAL', String) do |v|
params.canvas = v
end
parser.on('--xlim VAL', Array) do |v|
parser.on('--xlim VAL', Array, 'plotting range for the x coordinate') do |v|
params.xlim = v.take(2)
end
parser.on('--ylim VAL', Array) do |v|
parser.on('--ylim VAL', Array, 'plotting range for the y coordinate') do |v|
params.ylim = v.take(2)
end
@@ -167,10 +170,10 @@ module Uplot
parser.on('--canvas VAL', String) do |v|
params.canvas = v
end
parser.on('--xlim VAL', Array) do |v|
parser.on('--xlim VAL', Array, 'plotting range for the x coordinate') do |v|
params.xlim = v.take(2)
end
parser.on('--ylim VAL', Array) do |v|
parser.on('--ylim VAL', Array, 'plotting range for the y coordinate') do |v|
params.ylim = v.take(2)
end
@@ -178,15 +181,15 @@ module Uplot
parser.on('--grid', TrueClass) do |v|
params.grid = v
end
parser.on('--xlim VAL', Array) do |v|
parser.on('--xlim VAL', Array, 'plotting range for the x coordinate') do |v|
params.xlim = v.take(2)
end
parser.on('--ylim VAL', Array) do |v|
parser.on('--ylim VAL', Array, 'plotting range for the y coordinate') do |v|
params.ylim = v.take(2)
end
when :boxplot, :box
parser.on('--xlim VAL', Array) do |v|
parser.on('--xlim VAL', Array, 'plotting range for the x coordinate') do |v|
params.xlim = v.take(2)
end

View File

@@ -7,7 +7,7 @@ module Uplot
module Plot
module_function
def barplot(data, params, count = false)
def barplot(data, params, count: false)
headers = data.headers
series = data.series
if count
@@ -53,7 +53,7 @@ module Uplot
(method1.to_s + '!').to_sym
end
def xyy_plot(data, method1, params)
def plot_xyy(data, method1, params)
headers = data.headers
series = data.series
method2 = get_method2(method1)
@@ -70,7 +70,7 @@ module Uplot
plot
end
def xyxy_plot(data, method1, params)
def plot_xyxy(data, method1, params)
headers = data.headers
series = data.series
method2 = get_method2(method1)
@@ -87,34 +87,30 @@ module Uplot
plot
end
def lines(data, params, fmt = 'xyy')
check_series_size(data, fmt)
def plot_fmt(data, fmt, method1, params)
case fmt
when 'xyy'
xyy_plot(data, :lineplot, params)
plot_xyy(data, method1, params)
when 'xyxy'
xyxy_plot(data, :lineplot, params)
plot_xyxy(data, method1, params)
else
raise "Unknown format: #{fmt}"
end
end
def lines(data, params, fmt = 'xyy')
check_series_size(data, fmt)
plot_fmt(data, fmt, :lineplot, params)
end
def scatter(data, params, fmt = 'xyy')
check_series_size(data, fmt)
case fmt
when 'xyy'
xyy_plot(data, :scatterplot, params)
when 'xyxy'
xyxy_plot(data, :scatterplot, params)
end
plot_fmt(data, fmt, :scatterplot, params)
end
def density(data, params, fmt = 'xyy')
check_series_size(data, fmt)
case fmt
when 'xyy'
xyy_plot(data, :densityplot, params)
when 'xyxy'
xyxy_plot(data, :densityplot, params)
end
plot_fmt(data, fmt, :densityplot, params)
end
def boxplot(data, params)

View File

@@ -1,5 +1,5 @@
# frozen_string_literal: true
module Uplot
VERSION = '0.2.4'
VERSION = '0.2.6'
end

View File

@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
MSG
spec.homepage = 'https://github.com/kojix2/uplot'
spec.license = 'MIT'
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
spec.files = Dir['*.{md,txt}', '{lib,exe}/**/*']
spec.bindir = 'exe'