如何从文本文件值绘制图形?文本文件如下所示:
location count1 count2
HZ 100 193
ES 514 289
FP 70 137
BH 31 187
Run Code Online (Sandbox Code Playgroud)
我想将这些值绘制为 shell 脚本中的图形。在位置列的 x 轴值和 count1 和 count2 列的 y 轴值中。
使用相同的输入文件(ex.tsv),并创建一个 gnuplot 脚本以更好地控制细节
set style data histogram
set style fill solid border -1
plot for [i=2:3] '/dev/stdin' using i:xtic(1) title col
Run Code Online (Sandbox Code Playgroud)
和 gnuploting 数据:
gnuplot -p ex.gnu < ex.tsv
Run Code Online (Sandbox Code Playgroud)
我们看到对应的直方图。
要创建一个 png 文件(在 SO 中上传和显示),再添加 2 行:
set terminal pngcairo enhanced font "arial,10" fontscale 1.0 size 600, 400
set output 'out.png'
set style data histogram
set style fill solid border -1
plot for [i=2:3] '/dev/stdin' using i:xtic(1) title col
Run Code Online (Sandbox Code Playgroud)
gnuplot v5.0 的工作解决方案:
输入数据文件loc.dat:
location count1 count2
HZ 100 193
ES 514 289
FP 70 137
BH 31 187
Run Code Online (Sandbox Code Playgroud)
gnuplot脚本locations.plt:
#!/usr/bin/gnuplot -persist
set title "Location data"
set xlabel "location"
set ylabel "count"
set grid
plot "loc.dat" u (column(0)):2:xtic(1) w l title "","loc.dat" u (column(0)):3:xtic(1) w l title ""
Run Code Online (Sandbox Code Playgroud)
set title "Location data" - 主要情节标题
set xlabel "location"-x为轴设置标签
set ylabel "count"-y为轴设置标签
set grid - 在绘图中添加网格
(column(0)):2:xtic(1)- 列范围,(column(0))- 由于输入文件中的第一列具有非数字值,我们需要模仿数字第一列,因为 gnuplot 只需要其中的数字值
w l-手段用线,加入所有的数据点与线
互动启动:
$ gnuplot
gnuplot> load "locations.plt"
Run Code Online (Sandbox Code Playgroud)
渲染结果: