mirror of
https://github.com/red-data-tools/YouPlot.git
synced 2025-09-19 10:28:06 +08:00
Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
c73da80de6 | ||
![]() |
2e8641ccea | ||
![]() |
1b43f7d48f | ||
![]() |
d8396fecf9 | ||
![]() |
4660c2ab02 | ||
![]() |
d7e49f048f | ||
![]() |
34ae2b5815 | ||
![]() |
7e8dc6190c | ||
![]() |
ba105ab1f3 | ||
![]() |
96a1d1feb9 | ||
![]() |
2b65dae60c | ||
![]() |
943d4e6c44 | ||
![]() |
de33805c56 | ||
![]() |
de3a366d15 | ||
![]() |
4544c0e456 | ||
![]() |
ccfbaa7bde | ||
![]() |
3aceae9279 |
22
README.md
22
README.md
@@ -26,27 +26,7 @@ ruby -r numo/narray -e "puts Numo::DFloat.new(1000).rand_norm.to_a" \
|
|||||||
| uplot hist --nbins 15
|
| uplot hist --nbins 15
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
<img src="https://i.imgur.com/wpsoGJq.png" width="75%" height="75%"></img>
|
||||||
┌ ┐
|
|
||||||
[-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
|
|
||||||
```
|
|
||||||
|
|
||||||
**scatter**
|
**scatter**
|
||||||
|
|
||||||
|
@@ -25,10 +25,10 @@ module Uplot
|
|||||||
delimiter = parser.delimiter
|
delimiter = parser.delimiter
|
||||||
transpose = parser.transpose
|
transpose = parser.transpose
|
||||||
headers = parser.headers
|
headers = parser.headers
|
||||||
|
pass = parser.pass
|
||||||
output = parser.output
|
output = parser.output
|
||||||
count = parser.count
|
|
||||||
fmt = parser.fmt
|
fmt = parser.fmt
|
||||||
debug = parser.debug
|
@debug = parser.debug
|
||||||
|
|
||||||
if command == :colors
|
if command == :colors
|
||||||
Plot.colors
|
Plot.colors
|
||||||
@@ -36,33 +36,47 @@ module Uplot
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Sometimes the input file does not end with a newline code.
|
# Sometimes the input file does not end with a newline code.
|
||||||
while input = Kernel.gets(nil)
|
while (input = Kernel.gets(nil))
|
||||||
input.freeze
|
input.freeze
|
||||||
@raw_inputs << input
|
@raw_inputs << input
|
||||||
@data = Preprocessing.input(input, delimiter, headers, transpose)
|
@data = Preprocessing.input(input, delimiter, headers, transpose)
|
||||||
pp @data if @debug
|
pp @data if @debug
|
||||||
case command
|
plot = case command
|
||||||
when :bar, :barplot
|
when :bar, :barplot
|
||||||
Plot.barplot(data, params, @count)
|
Plot.barplot(data, params)
|
||||||
when :count, :c
|
when :count, :c
|
||||||
Plot.barplot(data, params, count = true)
|
Plot.barplot(data, params, count: true)
|
||||||
when :hist, :histogram
|
when :hist, :histogram
|
||||||
Plot.histogram(data, params)
|
Plot.histogram(data, params)
|
||||||
when :line, :lineplot
|
when :line, :lineplot
|
||||||
Plot.line(data, params)
|
Plot.line(data, params)
|
||||||
when :lines, :lineplots
|
when :lines, :lineplots
|
||||||
Plot.lines(data, params, fmt)
|
Plot.lines(data, params, fmt)
|
||||||
when :scatter, :s
|
when :scatter, :s
|
||||||
Plot.scatter(data, params, fmt)
|
Plot.scatter(data, params, fmt)
|
||||||
when :density, :d
|
when :density, :d
|
||||||
Plot.density(data, params, fmt)
|
Plot.density(data, params, fmt)
|
||||||
when :box, :boxplot
|
when :box, :boxplot
|
||||||
Plot.boxplot(data, params)
|
Plot.boxplot(data, params)
|
||||||
else
|
else
|
||||||
raise "unrecognized plot_type: #{command}"
|
raise "unrecognized plot_type: #{command}"
|
||||||
end.render($stderr)
|
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
|
end
|
||||||
end
|
end
|
||||||
|
@@ -2,6 +2,11 @@
|
|||||||
|
|
||||||
module Uplot
|
module Uplot
|
||||||
class Command
|
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(
|
Params = Struct.new(
|
||||||
# Sort me!
|
# Sort me!
|
||||||
:title,
|
:title,
|
||||||
|
@@ -7,7 +7,7 @@ module Uplot
|
|||||||
class Command
|
class Command
|
||||||
class Parser
|
class Parser
|
||||||
attr_reader :command, :params,
|
attr_reader :command, :params,
|
||||||
:delimiter, :transpose, :headers, :output, :count, :fmt, :debug
|
:delimiter, :transpose, :headers, :pass, :output, :fmt, :debug
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@command = nil
|
@command = nil
|
||||||
@@ -16,8 +16,8 @@ module Uplot
|
|||||||
@delimiter = "\t"
|
@delimiter = "\t"
|
||||||
@transpose = false
|
@transpose = false
|
||||||
@headers = nil
|
@headers = nil
|
||||||
@output = false
|
@pass = false
|
||||||
@count = false
|
@output = $stderr
|
||||||
@fmt = 'xyy'
|
@fmt = 'xyy'
|
||||||
@debug = false
|
@debug = false
|
||||||
end
|
end
|
||||||
@@ -26,49 +26,53 @@ module Uplot
|
|||||||
OptionParser.new do |opt|
|
OptionParser.new do |opt|
|
||||||
opt.program_name = 'uplot'
|
opt.program_name = 'uplot'
|
||||||
opt.version = Uplot::VERSION
|
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
|
@output = v
|
||||||
end
|
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
|
@delimiter = v
|
||||||
end
|
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
|
@headers = v
|
||||||
end
|
end
|
||||||
opt.on('-T', '--transpose', TrueClass) do |v|
|
opt.on('-T', '--transpose', TrueClass) do |v|
|
||||||
@transpose = v
|
@transpose = v
|
||||||
end
|
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
|
params.title = v
|
||||||
end
|
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
|
params.xlabel = v
|
||||||
end
|
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
|
params.ylabel = v
|
||||||
end
|
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
|
params.width = v
|
||||||
end
|
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
|
params.height = v
|
||||||
end
|
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
|
params.border = v.to_sym
|
||||||
end
|
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
|
params.margin = v
|
||||||
end
|
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
|
params.padding = v
|
||||||
end
|
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
|
params.color = v =~ /\A[0-9]+\z/ ? v.to_i : v.to_sym
|
||||||
end
|
end
|
||||||
opt.on('--[no-]labels', 'hide the labels', TrueClass) do |v|
|
opt.on('--[no-]labels', TrueClass, 'hide the labels') do |v|
|
||||||
params.labels = v
|
params.labels = v
|
||||||
end
|
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
|
@fmt = v
|
||||||
end
|
end
|
||||||
opt.on('--debug', TrueClass) do |v|
|
opt.on('--debug', TrueClass) do |v|
|
||||||
@@ -84,7 +88,9 @@ module Uplot
|
|||||||
main_parser.banner = \
|
main_parser.banner = \
|
||||||
<<~MSG
|
<<~MSG
|
||||||
Program: uplot (Tools for plotting on the terminal)
|
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]
|
Usage: uplot <command> [options]
|
||||||
|
|
||||||
@@ -115,40 +121,37 @@ module Uplot
|
|||||||
exit 1
|
exit 1
|
||||||
|
|
||||||
when :barplot, :bar
|
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
|
params.symbol = v
|
||||||
end
|
end
|
||||||
parser.on('--xscale VAL', String) do |v|
|
parser.on('--xscale VAL', String, 'axis scaling') do |v|
|
||||||
params.xscale = v
|
params.xscale = v
|
||||||
end
|
end
|
||||||
parser.on('--count', TrueClass) do |v|
|
|
||||||
@count = v
|
|
||||||
end
|
|
||||||
|
|
||||||
when :count, :c
|
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
|
params.symbol = v
|
||||||
end
|
end
|
||||||
|
|
||||||
when :histogram, :hist
|
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
|
params.nbins = v
|
||||||
end
|
end
|
||||||
parser.on('--closed VAL', String) do |v|
|
parser.on('--closed VAL', String) do |v|
|
||||||
params.closed = v
|
params.closed = v
|
||||||
end
|
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
|
params.symbol = v
|
||||||
end
|
end
|
||||||
|
|
||||||
when :lineplot, :line
|
when :lineplot, :line
|
||||||
parser.on('--canvas VAL', String) do |v|
|
parser.on('--canvas VAL', String, 'type of canvas') do |v|
|
||||||
params.canvas = v
|
params.canvas = v
|
||||||
end
|
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)
|
params.xlim = v.take(2)
|
||||||
end
|
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)
|
params.ylim = v.take(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -156,10 +159,10 @@ module Uplot
|
|||||||
parser.on('--canvas VAL', String) do |v|
|
parser.on('--canvas VAL', String) do |v|
|
||||||
params.canvas = v
|
params.canvas = v
|
||||||
end
|
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)
|
params.xlim = v.take(2)
|
||||||
end
|
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)
|
params.ylim = v.take(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -167,10 +170,10 @@ module Uplot
|
|||||||
parser.on('--canvas VAL', String) do |v|
|
parser.on('--canvas VAL', String) do |v|
|
||||||
params.canvas = v
|
params.canvas = v
|
||||||
end
|
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)
|
params.xlim = v.take(2)
|
||||||
end
|
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)
|
params.ylim = v.take(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -178,15 +181,15 @@ module Uplot
|
|||||||
parser.on('--grid', TrueClass) do |v|
|
parser.on('--grid', TrueClass) do |v|
|
||||||
params.grid = v
|
params.grid = v
|
||||||
end
|
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)
|
params.xlim = v.take(2)
|
||||||
end
|
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)
|
params.ylim = v.take(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
when :boxplot, :box
|
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)
|
params.xlim = v.take(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@@ -7,7 +7,7 @@ module Uplot
|
|||||||
module Plot
|
module Plot
|
||||||
module_function
|
module_function
|
||||||
|
|
||||||
def barplot(data, params, count = false)
|
def barplot(data, params, count: false)
|
||||||
headers = data.headers
|
headers = data.headers
|
||||||
series = data.series
|
series = data.series
|
||||||
if count
|
if count
|
||||||
@@ -53,7 +53,7 @@ module Uplot
|
|||||||
(method1.to_s + '!').to_sym
|
(method1.to_s + '!').to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
def xyy_plot(data, method1, params)
|
def plot_xyy(data, method1, params)
|
||||||
headers = data.headers
|
headers = data.headers
|
||||||
series = data.series
|
series = data.series
|
||||||
method2 = get_method2(method1)
|
method2 = get_method2(method1)
|
||||||
@@ -70,7 +70,7 @@ module Uplot
|
|||||||
plot
|
plot
|
||||||
end
|
end
|
||||||
|
|
||||||
def xyxy_plot(data, method1, params)
|
def plot_xyxy(data, method1, params)
|
||||||
headers = data.headers
|
headers = data.headers
|
||||||
series = data.series
|
series = data.series
|
||||||
method2 = get_method2(method1)
|
method2 = get_method2(method1)
|
||||||
@@ -87,34 +87,30 @@ module Uplot
|
|||||||
plot
|
plot
|
||||||
end
|
end
|
||||||
|
|
||||||
def lines(data, params, fmt = 'xyy')
|
def plot_fmt(data, fmt, method1, params)
|
||||||
check_series_size(data, fmt)
|
|
||||||
case fmt
|
case fmt
|
||||||
when 'xyy'
|
when 'xyy'
|
||||||
xyy_plot(data, :lineplot, params)
|
plot_xyy(data, method1, params)
|
||||||
when 'xyxy'
|
when 'xyxy'
|
||||||
xyxy_plot(data, :lineplot, params)
|
plot_xyxy(data, method1, params)
|
||||||
|
else
|
||||||
|
raise "Unknown format: #{fmt}"
|
||||||
end
|
end
|
||||||
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')
|
def scatter(data, params, fmt = 'xyy')
|
||||||
check_series_size(data, fmt)
|
check_series_size(data, fmt)
|
||||||
case fmt
|
plot_fmt(data, fmt, :scatterplot, params)
|
||||||
when 'xyy'
|
|
||||||
xyy_plot(data, :scatterplot, params)
|
|
||||||
when 'xyxy'
|
|
||||||
xyxy_plot(data, :scatterplot, params)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def density(data, params, fmt = 'xyy')
|
def density(data, params, fmt = 'xyy')
|
||||||
check_series_size(data, fmt)
|
check_series_size(data, fmt)
|
||||||
case fmt
|
plot_fmt(data, fmt, :densityplot, params)
|
||||||
when 'xyy'
|
|
||||||
xyy_plot(data, :densityplot, params)
|
|
||||||
when 'xyxy'
|
|
||||||
xyxy_plot(data, :densityplot, params)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def boxplot(data, params)
|
def boxplot(data, params)
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Uplot
|
module Uplot
|
||||||
VERSION = '0.2.4'
|
VERSION = '0.2.6'
|
||||||
end
|
end
|
||||||
|
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
|
|||||||
MSG
|
MSG
|
||||||
spec.homepage = 'https://github.com/kojix2/uplot'
|
spec.homepage = 'https://github.com/kojix2/uplot'
|
||||||
spec.license = 'MIT'
|
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.files = Dir['*.{md,txt}', '{lib,exe}/**/*']
|
||||||
spec.bindir = 'exe'
|
spec.bindir = 'exe'
|
||||||
|
Reference in New Issue
Block a user