From e11b5047afb80bdda7f0460c9ce67fae41b21655 Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Wed, 2 Dec 2020 18:24:22 +0900 Subject: [PATCH] Add yx format A new format option for barplot and lineplot. Use when the first column is the label and the second column is the value. --- lib/youplot/backends/unicode_plot_backend.rb | 46 ++++++++++++++------ lib/youplot/command.rb | 4 +- lib/youplot/command/parser.rb | 18 ++++++-- 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/lib/youplot/backends/unicode_plot_backend.rb b/lib/youplot/backends/unicode_plot_backend.rb index 16ace0b..5497b73 100644 --- a/lib/youplot/backends/unicode_plot_backend.rb +++ b/lib/youplot/backends/unicode_plot_backend.rb @@ -9,7 +9,7 @@ module YouPlot module UnicodePlotBackend module_function - def barplot(data, params, count: false) + def barplot(data, params, fmt = nil, count: false) headers = data.headers series = data.series # `uplot count` @@ -18,14 +18,24 @@ module YouPlot params.title = headers[0] if headers end if series.size == 1 - # If there is only one series, use the line number for label. + # If there is only one series.use the line number for label. params.title ||= headers[0] if headers labels = Array.new(series[0].size) { |i| (i + 1).to_s } values = series[0].map(&:to_f) else - params.title ||= headers[1] if headers - labels = series[0] - values = series[1].map(&:to_f) + # If there are 2 or more series... + if fmt == 'yx' + # assume that the first 2 series are the y and x series respectively. + x_col = 1 + y_col = 0 + else + # assume that the first 2 series are the x and y series respectively. + x_col = 0 + y_col = 1 + end + params.title ||= headers[y_col] if headers + labels = series[x_col] + values = series[y_col].map(&:to_f) end UnicodePlot.barplot(labels, values, **params.to_hc) end @@ -38,7 +48,7 @@ module YouPlot UnicodePlot.histogram(values, **params.to_hc) end - def line(data, params) + def line(data, params, fmt = nil) headers = data.headers series = data.series if series.size == 1 @@ -47,14 +57,22 @@ module YouPlot y = series[0].map(&:to_f) UnicodePlot.lineplot(y, **params.to_hc) else - # If there are 2 or more series, - # assume that the first 2 series are the x and y series respectively. - if headers - params.xlabel ||= headers[0] - params.ylabel ||= headers[1] + # If there are 2 or more series... + if fmt == 'yx' + # assume that the first 2 series are the y and x series respectively. + x_col = 1 + y_col = 0 + else + # assume that the first 2 series are the x and y series respectively. + x_col = 0 + y_col = 1 end - x = series[0].map(&:to_f) - y = series[1].map(&:to_f) + if headers + params.xlabel ||= headers[x_col] + params.ylabel ||= headers[y_col] + end + x = series[x_col].map(&:to_f) + y = series[y_col].map(&:to_f) UnicodePlot.lineplot(x, y, **params.to_hc) end end @@ -103,6 +121,8 @@ module YouPlot plot_xyy(data, method1, params) when 'xyxy' plot_xyxy(data, method1, params) + when 'yx' + raise "Incorrect format: #{fmt}" else raise "Unknown format: #{fmt}" end diff --git a/lib/youplot/command.rb b/lib/youplot/command.rb index 62e574e..f935d5a 100644 --- a/lib/youplot/command.rb +++ b/lib/youplot/command.rb @@ -64,13 +64,13 @@ module YouPlot plot = case command when :bar, :barplot - @backend.barplot(data, params) + @backend.barplot(data, params, fmt) when :count, :c @backend.barplot(data, params, count: true) when :hist, :histogram @backend.histogram(data, params) when :line, :lineplot - @backend.line(data, params) + @backend.line(data, params, fmt) when :lines, :lineplots @backend.lines(data, params, fmt) when :scatter, :s diff --git a/lib/youplot/command/parser.rb b/lib/youplot/command/parser.rb index 295ebaf..ece488b 100644 --- a/lib/youplot/command/parser.rb +++ b/lib/youplot/command/parser.rb @@ -79,9 +79,6 @@ module YouPlot opt.on('--[no-]labels', TrueClass, 'hide the labels') do |v| params.labels = v end - 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('--encoding VAL', String, 'Specify the input encoding') do |v| @encoding = v end @@ -165,6 +162,9 @@ module YouPlot parser.on_head('--xscale VAL', String, 'axis scaling') do |v| params.xscale = v end + parser.on_head('--fmt VAL', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v| + @fmt = v + end when :count, :c parser.on_head('--symbol VAL', String, 'character to be used to plot the bars') do |v| @@ -192,6 +192,9 @@ module YouPlot parser.on_head('--ylim VAL', Array, 'plotting range for the y coordinate') do |v| params.ylim = v.take(2) end + parser.on_head('--fmt VAL', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v| + @fmt = v + end when :lineplots, :lines parser.on_head('--canvas VAL', String) do |v| @@ -203,6 +206,9 @@ module YouPlot parser.on_head('--ylim VAL', Array, 'plotting range for the y coordinate') do |v| params.ylim = v.take(2) end + parser.on_head('--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 when :scatter, :s parser.on_head('--canvas VAL', String) do |v| @@ -214,6 +220,9 @@ module YouPlot parser.on_head('--ylim VAL', Array, 'plotting range for the y coordinate') do |v| params.ylim = v.take(2) end + parser.on_head('--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 when :density, :d parser.on_head('--grid', TrueClass) do |v| @@ -225,6 +234,9 @@ module YouPlot parser.on_head('--ylim VAL', Array, 'plotting range for the y coordinate') do |v| params.ylim = v.take(2) end + parser.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 when :boxplot, :box parser.on_head('--xlim VAL', Array, 'plotting range for the x coordinate') do |v|