Abd*_*red 47 command-line graphics
如果有一个长文本文件并且我想显示给定模式出现的所有行,我会这样做:
grep -n form innsmouth.txt | cut -d : -f1
Run Code Online (Sandbox Code Playgroud)
现在,我有一个数字序列(每行一个数字)
我想用 x 轴上的出现和 y 轴上的行号进行 2D 图形表示。我怎样才能做到这一点?

Nat*_*dge 58
您可以gnuplot为此使用:
primes 1 100 |gnuplot -p -e 'plot "/dev/stdin"'
Run Code Online (Sandbox Code Playgroud)
产生类似的东西

您可以根据自己的喜好配置图形的外观,以各种图像格式输出等。
ter*_*don 15
我会在R. 您必须安装它,但它应该在您的发行版存储库中可用。对于基于 Debian 的系统,运行
sudo apt-get install r-base
Run Code Online (Sandbox Code Playgroud)
这也应该带来,r-base-core但如果没有,也运行sudo apt-get install r-base-core。一旦你已经R安装了,你可以写这样的简易R脚本:
#!/usr/bin/env Rscript
args <- commandArgs(TRUE)
## Read the input data
a<-read.table(args[1])
## Set the output file name/type
pdf(file="output.pdf")
## Plot your data
plot(a$V2,a$V1,ylab="line number",xlab="value")
## Close the graphics device (write to the output file)
dev.off()
Run Code Online (Sandbox Code Playgroud)
上面的脚本将创建一个名为output.pdf. 我测试如下:
## Create a file with 100 random numbers and add line numbers (cat -n)
for i in {1..100}; do echo $RANDOM; done | cat -n > file
## Run the R script
./foo.R file
Run Code Online (Sandbox Code Playgroud)
在我使用的随机数据上,产生:

我不完全确定您想要绘制什么,但这至少应该为您指明正确的方向。
mik*_*erv 13
如果可能是一个非常简单的终端打印输出就足够了,并且您可以通过倒轴来满足,请考虑以下事项:
seq 1000 |
grep -n 11 |
while IFS=: read -r n match
do printf "%0$((n/10))s\n" "$match"
done
Run Code Online (Sandbox Code Playgroud)
上面图表的图案的每次出现10%的比例在倒置趋势11中的输出seq 1000。
像这样:
11
110
111
112
113
114
115
116
117
118
119
211
311
411
511
611
711
811
911
Run Code Online (Sandbox Code Playgroud)
有了点和出现次数,它可能是:
seq 1000 |
grep -n 11 | {
i=0
while IFS=: read -r n match
do printf "%02d%0$((n/10))s\n" "$((i+=1))" .
done; }
Run Code Online (Sandbox Code Playgroud)
...打印...
01 .
02 .
03 .
04 .
05 .
06 .
07 .
08 .
09 .
10 .
11 .
12 .
13 .
14 .
15 .
16 .
17 .
18 .
19 .
Run Code Online (Sandbox Code Playgroud)
您可以通过更多的工作来获得像您的示例一样的轴,并且tput- 您需要进行\033[A转义(或与您的终端模拟器兼容的等效项),以便每次出现时将光标向上移动一行。
如果awk'sprintf像 POSIX-shell 一样支持空格填充printf,那么您可以使用它来做同样的事情 - 而且可能效率更高。但是,我不知道如何使用awk.
小智 7
检查允许使用 python3 在终端上直接绘制数据的包plotext。它非常直观,因为它的使用与matplotlib包非常相似。
这是一个基本示例:
您可以使用以下命令安装它:
sudo -H pip install plotext
Run Code Online (Sandbox Code Playgroud)
至于matplotlib,主要功能是scatter(对于单点)、plot(对于线连接的点)和show(在终端上实际打印绘图)。很容易指定绘图尺寸、点和线样式以及显示轴、数字刻度和最终方程的任何内容,用于将绘制的坐标转换为原始实际值。
这是生成上面显示的图的代码:
import plotext.plot as plx
import numpy as np
l=3000
x=np.arange(0, l)
y=np.sin(4*np.pi/l*np.array(x))*np.exp(-0.5*np.pi/l*x)
plx.scatter(x, y, rows = 17, cols = 70)
plx.show(clear = 0)
Run Code Online (Sandbox Code Playgroud)
clear=True里面的选项show用于在绘图前清除终端:这很有用,例如,在绘制连续的数据流时。绘制连续数据流的示例如下所示:
该包描述提供了更多的信息,如何自定义曲线。该软件包已在 Ubuntu 16 上进行了测试,可以完美运行。未来可能的发展(根据要求)可能涉及对 python2 和其他图形界面(例如 jupiter)的扩展。如果您在使用时遇到任何问题,请告诉我。谢谢。
我希望这能解决你的问题。