mirror of
https://github.com/red-data-tools/YouPlot.git
synced 2025-09-19 02:18:08 +08:00
Compare commits
22 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
e76d4f279e | ||
![]() |
3a1a29424d | ||
![]() |
b78da2388a | ||
![]() |
a7bb75e87d | ||
![]() |
ca9f97a7dc | ||
![]() |
f67a5ce913 | ||
![]() |
dbbfd366be | ||
![]() |
42cadee553 | ||
![]() |
e689c69838 | ||
![]() |
c53169e9ee | ||
![]() |
95a35399a2 | ||
![]() |
7c00f95962 | ||
![]() |
fcbf9305d7 | ||
![]() |
04eddfd79b | ||
![]() |
125c2291d5 | ||
![]() |
6a72418d01 | ||
![]() |
72a56659de | ||
![]() |
858792fa99 | ||
![]() |
e7079c56a2 | ||
![]() |
bb8a16a327 | ||
![]() |
be0f706231 | ||
![]() |
e5c87fbe6f |
3
.github/workflows/ci.yml
vendored
3
.github/workflows/ci.yml
vendored
@@ -13,6 +13,5 @@ jobs:
|
||||
- uses: ruby/setup-ruby@v1
|
||||
with:
|
||||
ruby-version: ${{ matrix.ruby }}
|
||||
- run: gem install bundler
|
||||
- run: bundle install
|
||||
bundler-cache: true
|
||||
- run: bundle exec rake test
|
||||
|
6
Gemfile
6
Gemfile
@@ -4,3 +4,9 @@ source 'https://rubygems.org'
|
||||
|
||||
# Specify your gem's dependencies in youplot.gemspec
|
||||
gemspec
|
||||
|
||||
group :test do
|
||||
gem 'rake'
|
||||
gem 'simplecov'
|
||||
gem 'test-unit'
|
||||
end
|
||||
|
143
README.md
143
README.md
@@ -1,18 +1,15 @@
|
||||
<div align="center">
|
||||
<img src="logo.svg" width="66%" height="66%" />
|
||||
|
||||
<img src="logo.svg">
|
||||
<hr>
|
||||
<img alt="Build Status" src="https://github.com/red-data-tools/YouPlot/workflows/test/badge.svg">
|
||||
<a href="https://rubygems.org/gems/youplot/"><img alt="Gem Version" src="https://badge.fury.io/rb/youplot.svg"></a>
|
||||
<a href="https://zenodo.org/badge/latestdoi/283230219"><img alt="DOI" src="https://zenodo.org/badge/283230219.svg"></a>
|
||||
<a href="https://rubydoc.info/gems/youplot/"><img alt="Docs Stable" src="https://img.shields.io/badge/docs-stable-blue.svg"></a>
|
||||
<a href="LICENSE.txt"><img alt="The MIT License" src="https://img.shields.io/badge/license-MIT-blue.svg"></a>
|
||||
|
||||

|
||||
[](https://badge.fury.io/rb/youplot)
|
||||
[](https://rubydoc.info/gems/youplot)
|
||||
[](LICENSE.txt)
|
||||
[](https://zenodo.org/badge/latestdoi/283230219)
|
||||
|
||||
YouPlot is a command line tool that draws plots in a terminal.
|
||||
YouPlot is a command line tool that draws plots on the terminal.
|
||||
|
||||
:bar_chart: Powered by [UnicodePlot](https://github.com/red-data-tools/unicode_plot.rb)
|
||||
|
||||
</div>
|
||||
|
||||
## Installation
|
||||
@@ -23,8 +20,9 @@ gem install youplot
|
||||
|
||||
## Quick Start
|
||||
|
||||
* `cat data.tsv | uplot <command> [options]` or
|
||||
* `uplot <command> [options] <data.tsv>`
|
||||
<img alt="barplot" src="https://user-images.githubusercontent.com/5798442/101999903-d36a2d00-3d24-11eb-9361-b89116f44122.png" width=160> <img alt="histogram" src="https://user-images.githubusercontent.com/5798442/101999820-21cafc00-3d24-11eb-86db-e410d19b07df.png" width=160> <img alt="scatter" src="https://user-images.githubusercontent.com/5798442/101999827-27284680-3d24-11eb-9903-551857eaa69c.png" width=160> <img alt="density" src="https://user-images.githubusercontent.com/5798442/101999828-2abbcd80-3d24-11eb-902c-2f44266fa6ae.png" width=160> <img alt="boxplot" src="https://user-images.githubusercontent.com/5798442/101999830-2e4f5480-3d24-11eb-8891-728c18bf5b35.png" width=160>
|
||||
|
||||
`uplot <command> [options] <data.tsv>`
|
||||
|
||||
### barplot
|
||||
|
||||
@@ -36,7 +34,7 @@ curl -sL https://git.io/ISLANDScsv \
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<img alt="barplot" src="https://user-images.githubusercontent.com/5798442/101999903-d36a2d00-3d24-11eb-9361-b89116f44122.png">
|
||||
<img alt="barplot" src="https://user-images.githubusercontent.com/5798442/101999903-d36a2d00-3d24-11eb-9361-b89116f44122.png">
|
||||
</p>
|
||||
|
||||
### histogram
|
||||
@@ -103,10 +101,6 @@ curl -sL https://git.io/IRIStsv \
|
||||
|
||||
### count
|
||||
|
||||
In this example, YouPlot counts the number of chromosomes where the gene is located from the human gene annotation file and it creates a bar chart. The human gene annotation file can be downloaded from the following website.
|
||||
|
||||
* https://www.gencodegenes.org/human/
|
||||
|
||||
```sh
|
||||
cat gencode.v35.annotation.gff3 \
|
||||
| grep -v '#' | grep 'gene' | cut -f1 \
|
||||
@@ -117,23 +111,21 @@ cat gencode.v35.annotation.gff3 \
|
||||
<img alt="count" src="https://user-images.githubusercontent.com/5798442/101999832-30b1ae80-3d24-11eb-96fe-e5000bed1f5c.png">
|
||||
</p>
|
||||
|
||||
In this example, YouPlot counts the number of chromosomes where genes are located.
|
||||
* [GENCODE - Human Release](https://www.gencodegenes.org/human/)
|
||||
|
||||
Note: `count` is not very fast because it runs in a Ruby script.
|
||||
This is fine in most cases, as long as the data size is small. If you want to visualize huge data, it is faster to use a combination of common Unix commands as shown below.
|
||||
|
||||
```sh
|
||||
cat gencode.v35.annotation.gff3 | grep -v '#' | grep 'gene' | cut -f1 \
|
||||
| sort | uniq -c | sort -nrk2 | awk '{print $2,$1}' \
|
||||
| uplot bar -d ' ' -t "The number of human gene annotations per chromosome" -c blue
|
||||
| sort | uniq -c | sort -nrk1 \
|
||||
| uplot bar --fmt yx -d ' ' -t "The number of human gene annotations per chromosome" -c blue
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Why YouPlot?
|
||||
|
||||
Wouldn't it be a pain to have to run R, Python, Julia, gnuplot or whatever REPL just to check your data?
|
||||
YouPlot is a command line tool for this purpose. With YouPlot, you can continue working without leaving your terminal and shell.
|
||||
|
||||
### How to use YouPlot?
|
||||
### Commands
|
||||
|
||||
`uplot` is the shortened form of `youplot`. You can use either.
|
||||
|
||||
@@ -143,18 +135,7 @@ YouPlot is a command line tool for this purpose. With YouPlot, you can continue
|
||||
| `uplot <command> [options] data.tsv ...` | Take input from files |
|
||||
| `pipeline1 \| uplot <command> -O \| pipeline2` | Outputs data from stdin to stdout |
|
||||
|
||||
### Where to output the plot?
|
||||
|
||||
By default, the plot is output to *standard error output*.
|
||||
The output file or stream for the plot can be specified with the `-o` option.
|
||||
|
||||
### Where to output the input data?
|
||||
|
||||
By default, the input data is not shown anywhere.
|
||||
The `-O` option, with no arguments, outputs the input data directly to the standard output.
|
||||
This is useful when passing data to a subsequent pipeline.
|
||||
|
||||
### What types of plots are available?
|
||||
### Subcommands
|
||||
|
||||
The following sub-commands are available.
|
||||
|
||||
@@ -167,66 +148,75 @@ The following sub-commands are available.
|
||||
| scatter | s | draw a scatter plot |
|
||||
| density | d | draw a density plot |
|
||||
| boxplot | box | draw a horizontal boxplot |
|
||||
| | | |
|
||||
| count | c | draw a barplot based on the number of occurrences (slow) |
|
||||
| | | |
|
||||
| colors | color | show the list of available colors |
|
||||
|
||||
See Quick Start for `count`.
|
||||
### Output the plot
|
||||
|
||||
| command | short | how it works |
|
||||
|-----------|-------|----------------------------------------------------------|
|
||||
| count | c | draw a barplot based on the number of occurrences (slow) |
|
||||
* `-o`
|
||||
* By default, the plot is output to **standard error output**.
|
||||
* If you want to output to standard input, Use hyphen ` -o -` or no argument `uplot s -o | `.
|
||||
|
||||
### What if the header line is included?
|
||||
### Output the input data
|
||||
|
||||
If your input data contains a header line, you need to specify the `-H` option.
|
||||
* `-O`
|
||||
* By default, the input data is not shown anywhere.
|
||||
* If you want to pass the input data directly to the standard output, Use hyphen `-O -` or no argument `uplot s -O |`.
|
||||
* This is useful when passing data to a subsequent pipeline.
|
||||
|
||||
### How to specify the delimiter?
|
||||
### Header
|
||||
|
||||
Use the `-d` option. To specify a blank space, you can use `uplot bar -d ' ' data.txt`.
|
||||
You do not need to use `-d` option for tab-delimited text since the default value is tab.
|
||||
* `-H`
|
||||
* If input data contains a header line, you need to specify the `-H` option.
|
||||
|
||||
### Is there a way to specify a column as the x-axis or y-axis?
|
||||
### Delimiter
|
||||
|
||||
Not yet.
|
||||
YouPlot treats the first column as the X axis and the second column as the Y axis.
|
||||
When working with multiple series, the first column is the X axis, the second column is series Y1, the third column is series Y2, and so on.
|
||||
If you pass only one column of data for `line` and `bar`, YouPlot will automatically use a sequential number starting from 1 as the X-axis.
|
||||
* `-d`
|
||||
* You do not need to use `-d` option for tab-delimited text since the default value is tab.
|
||||
* To specify a blank space, you can use `uplot bar -d ' ' data.txt`.
|
||||
|
||||
* `--fmt xyy` `--fmt xyxy` `--fmt yx` options give you a few more choices.
|
||||
See `youplot <command> --help` for more details.
|
||||
### Real-time data
|
||||
|
||||
* Use `awk '{print $2, $1}'` to swap lines.
|
||||
* Use `paste` to concatenate series.
|
||||
* `-p` `--progress`
|
||||
* Experimental progressive mode is currently under development.
|
||||
* `ruby -e 'loop{puts rand(100)}' | uplot line --progress`
|
||||
|
||||
### How to plot real-time data?
|
||||
### Show detailed options for subcommands
|
||||
|
||||
Experimental progressive mode is currently under development.
|
||||
* `--help`
|
||||
* The `--help` option will show more detailed options for each subcommand.
|
||||
* `uplot hist --help`
|
||||
|
||||
```sh
|
||||
ruby -e 'loop{puts rand(100)}' | uplot line --progress
|
||||
```
|
||||
### Set columns as x-axis or y-axis
|
||||
|
||||
### How to view detailed command line options?
|
||||
* YouPlot treats the first column as the X axis and the second column as the Y axis. When working with multiple series, the first column is the X axis, the second column is series Y1, the third column is series Y2, and so on.
|
||||
* If you pass only one column of data for `line` and `bar`, YouPlot will automatically use a sequential number starting from 1 as the X-axis.
|
||||
|
||||
Use `--help` to print command-specific options.
|
||||
* `--fmt`
|
||||
* `--fmt xyy` `--fmt xyxy` `--fmt yx` options give you a few more choices. See `youplot <command> --help` for more details.
|
||||
* The fmt option may be renamed in the future.
|
||||
* The `-x` and `-y` options might be used to specify columns in the future.
|
||||
|
||||
`uplot hist --help`
|
||||
* Use `awk '{print $2, $1}'` to swap columns. Use `paste` to concatenate series.
|
||||
|
||||
```
|
||||
Usage: uplot histogram [options] <in.tsv>
|
||||
### Categorical data
|
||||
|
||||
Options for histogram:
|
||||
--symbol VAL character to be used to plot the bars
|
||||
--closed VAL side of the intervals to be closed [left]
|
||||
-n, --nbins VAL approximate number of bins
|
||||
* With GNU datamash, you can manage to handle categorized data.
|
||||
* `cat test/fixtures/iris.csv | sed '/^$/d' | datamash --header-in --output-delimiter=: -t, -g5 collapse 3,4 | cut -f2-3 -d: | sed 's/:/\n/g' | uplot s -d, -T --fmt xyxy`
|
||||
* This is not so easy...
|
||||
|
||||
Options:
|
||||
...
|
||||
```
|
||||
### Time series
|
||||
|
||||
### How to view the list of available colors?
|
||||
* Not yet supported.
|
||||
|
||||
```sh
|
||||
uplot colors
|
||||
```
|
||||
## Tools that are useful to use with YouPlot
|
||||
|
||||
* [csvtk](https://github.com/shenwei356/csvtk)
|
||||
* [GNU datamash](https://www.gnu.org/software/datamash/)
|
||||
* [awk](https://www.gnu.org/software/gawk/)
|
||||
* [xsv](https://github.com/BurntSushi/xsv)
|
||||
|
||||
## Contributing
|
||||
|
||||
@@ -248,6 +238,7 @@ git clone https://github.com/your_name/YouPlot
|
||||
bundle install # Install the gem dependencies
|
||||
bundle exec rake test # Run the test
|
||||
bundle exec rake install # Installation from source code
|
||||
bundle exec exe/uplot # Run youplot (Try out the edited code)
|
||||
```
|
||||
|
||||
### Acknowledgements
|
||||
|
@@ -6,6 +6,12 @@ require_relative 'youplot/parameters'
|
||||
require_relative 'youplot/command'
|
||||
|
||||
module YouPlot
|
||||
# @run_as_executable = true / false
|
||||
# YouPlot behaves slightly differently when run as a command line tool
|
||||
# and when run as a script (e.g. for testing). In the event of an error,
|
||||
# when run as a command line tool, YouPlot will display a short error message
|
||||
# and exit abnormally. When run as a script, it will just raise an error.
|
||||
@run_as_executable = false
|
||||
class << self
|
||||
attr_accessor :run_as_executable
|
||||
|
||||
@@ -13,5 +19,4 @@ module YouPlot
|
||||
@run_as_executable
|
||||
end
|
||||
end
|
||||
@run_as_executable = false
|
||||
end
|
||||
|
@@ -6,6 +6,27 @@
|
||||
require_relative 'processing'
|
||||
require 'unicode_plot'
|
||||
|
||||
# If the line color is specified as a number, the program will display an error
|
||||
# message to the user and exit. Remove this patch when UnicodePlot is improved.
|
||||
|
||||
module UnicodePlot
|
||||
class << self
|
||||
alias lineplot_original lineplot
|
||||
def lineplot(*args, **kw)
|
||||
if kw[:color].is_a? Numeric
|
||||
warn <<~EOS
|
||||
YouPlot: Line colors cannot be specified by numerical values.
|
||||
|
||||
For more information, please see the following issue.
|
||||
https://github.com/red-data-tools/unicode_plot.rb/issues/34
|
||||
EOS
|
||||
YouPlot.run_as_executable ? exit(1) : raise(Error)
|
||||
end
|
||||
lineplot_original(*args, **kw)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module YouPlot
|
||||
# plotting functions.
|
||||
module Backends
|
||||
@@ -106,11 +127,10 @@ module YouPlot
|
||||
|
||||
def plot_xyxy(data, method1, params)
|
||||
headers = data.headers
|
||||
series = data.series
|
||||
series2 = data.series
|
||||
.map { |s| s.map(&:to_f) }
|
||||
.each_slice(2).to_a
|
||||
method2 = get_method2(method1)
|
||||
series.map! { |s| s.map(&:to_f) }
|
||||
series2 = series.each_slice(2).to_a
|
||||
series = nil
|
||||
params.name ||= headers[0] if headers
|
||||
params.xlim ||= series2.map(&:first).flatten.minmax # why need?
|
||||
params.ylim ||= series2.map(&:last).flatten.minmax # why need?
|
||||
@@ -182,7 +202,7 @@ module YouPlot
|
||||
series = data.series
|
||||
if series.size == 1
|
||||
warn <<~EOS
|
||||
youplot: There is only one series of input data. Please check the delimiter.
|
||||
YouPlot: There is only one series of input data. Please check the delimiter.
|
||||
|
||||
Headers: \e[35m#{data.headers.inspect}\e[0m
|
||||
The first item is: \e[35m\"#{series[0][0]}\"\e[0m
|
||||
|
@@ -169,7 +169,7 @@ module YouPlot
|
||||
def output_data(input)
|
||||
# Pass the input to subsequent pipelines
|
||||
case options[:pass]
|
||||
when IO
|
||||
when IO, StringIO
|
||||
options[:pass].print(input)
|
||||
else
|
||||
if options[:pass]
|
||||
|
@@ -58,10 +58,10 @@ module YouPlot
|
||||
parser.on('-t', '--title STR', String, 'print string on the top of plot') do |v|
|
||||
params.title = v
|
||||
end
|
||||
parser.on('-x', '--xlabel STR', String, 'print string on the bottom of the plot') do |v|
|
||||
parser.on('--xlabel STR', String, 'print string on the bottom of the plot') do |v|
|
||||
params.xlabel = v
|
||||
end
|
||||
parser.on('-y', '--ylabel STR', String, 'print string on the far left of the plot') do |v|
|
||||
parser.on('--ylabel STR', String, 'print string on the far left of the plot') do |v|
|
||||
params.ylabel = v
|
||||
end
|
||||
parser.on('-w', '--width INT', Numeric, 'number of characters per row') do |v|
|
||||
@@ -95,7 +95,7 @@ module YouPlot
|
||||
parser.on('-M', '--monochrome', TrueClass, 'no colouring even if writing to a tty') do |_v|
|
||||
UnicodePlot::IOContext.define_method(:color?) { false } # FIXME
|
||||
end
|
||||
parser.on('--encoding STR', String, 'Specify the input encoding') do |v|
|
||||
parser.on('--encoding STR', String, 'specify the input encoding') do |v|
|
||||
options[:encoding] = v
|
||||
end
|
||||
# Optparse adds the help option, but it doesn't show up in usage.
|
||||
@@ -130,10 +130,9 @@ module YouPlot
|
||||
scatter s draw a scatter plot
|
||||
density d draw a density plot
|
||||
boxplot box draw a horizontal boxplot
|
||||
colors color show the list of available colors
|
||||
|
||||
count c draw a baplot based on the number of
|
||||
occurrences (slow)
|
||||
colors color show the list of available colors
|
||||
|
||||
General options:
|
||||
--help print command specific help menu
|
||||
|
@@ -1,5 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module YouPlot
|
||||
VERSION = '0.4.2'
|
||||
VERSION = '0.4.3'
|
||||
end
|
||||
|
@@ -155,21 +155,11 @@ class YouPlotSimpleTest < Test::Unit::TestCase
|
||||
assert_equal fixture('simple-lineplot-xlabel.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :line_x do
|
||||
YouPlot::Command.new(['line', '-x', 'X-LABEL']).run
|
||||
assert_equal fixture('simple-lineplot-xlabel.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :line_ylabel do
|
||||
YouPlot::Command.new(['line', '--ylabel', 'Y-LABEL']).run
|
||||
assert_equal fixture('simple-lineplot-ylabel.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :line_y do
|
||||
YouPlot::Command.new(['line', '-y', 'Y-LABEL']).run
|
||||
assert_equal fixture('simple-lineplot-ylabel.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :line_width do
|
||||
YouPlot::Command.new(['line', '--width', '17']).run
|
||||
assert_equal fixture('simple-lineplot-width-17.txt'), @stderr_file.read
|
||||
|
@@ -19,10 +19,5 @@ Gem::Specification.new do |spec|
|
||||
spec.executables = %w[uplot youplot]
|
||||
spec.require_paths = ['lib']
|
||||
|
||||
spec.add_runtime_dependency 'unicode_plot'
|
||||
spec.add_development_dependency 'bundler'
|
||||
spec.add_development_dependency 'rake'
|
||||
spec.add_development_dependency 'rubocop'
|
||||
spec.add_development_dependency 'simplecov'
|
||||
spec.add_development_dependency 'test-unit'
|
||||
spec.add_dependency 'unicode_plot', '>= 0.0.5'
|
||||
end
|
||||
|
Reference in New Issue
Block a user