如何让gnuplot 4.0自动使用顶行条目作为标题?

时间:2012-01-30 15:34:56

标签: bash gnuplot

我使用title column,columheader等找到了GNUPlot 4.2+这个问题的很多解决方案。但是,我坚持使用GNUPlot 4.0并且需​​要找到一种方法来使用我的数据集的顶行自动设置标题。 / p>

我正在使用bash脚本生成带有数据的动态( - >每日更改)文件,我有一个包含gnuplot设置的静态.gnu文件。

你有什么建议吗?

(也许在.gnu文件中使用变量/数组的方法?)

1 个答案:

答案 0 :(得分:1)

诀窍是使用Command Substitution

示例代码:

# content of file
$ cat data.txt
header
1
2
3
4
5

# generate gnuplot script
$ echo "set term dumb; plot '<(tail -n +2 data.txt)' title '$(head -1 data.txt)'"
set term dumb; plot '<(tail -n +2 data.txt)' title 'header'

# pipe to gnuplot 
$ echo "set term dumb; plot '<(tail -n +2 data.txt)' title '$(head -1 data.txt)'" | gnuplot

# or you can use `here-doc` (note: `>` is bash prompt)
$ gnuplot <<_EOF_
>    set term dumb
>    plot '<(tail -n +2 data.txt)' title '$(head -1 data.txt)'
>_EOF_

    5 ++-------+--------+-------+--------+--------+--------+-------+-------+A
      +        +        +       +        +        +        +  header   A    +
  4.5 ++                                                                   ++
      |                                                                     |
      |                                                                     |
    4 ++                                                   A               ++
      |                                                                     |
  3.5 ++                                                                   ++
      |                                                                     |
      |                                                                     |
    3 ++                                 A                                 ++
      |                                                                     |
  2.5 ++                                                                   ++
      |                                                                     |
      |                                                                     |
    2 ++                A                                                  ++
      |                                                                     |
  1.5 ++                                                                   ++
      |                                                                     |
      +        +        +       +        +        +        +       +        +
    1 A+-------+--------+-------+--------+--------+--------+-------+-------++
      0       0.5       1      1.5       2       2.5       3      3.5       4

注意: $(head -1 data.txt)在转到Gnuplot之前展开到header