2012-07-16 55 views
8

繪製列我有這種格式的文件:通過調用它們的頭與GNUPLOT

x y1 y2 y3 ei1 ei2 ei3 es1 es2 es3 
1 4 5 4 7 7 2 4 7 7 
2 7 3 3 3 8 3 3 3 8 
3 2 1 4 4 9 6 4 4 9 

我想產生類似於下面的命令將 給

plot "filename" using 1:2:5:8 with yerrorbars 

地塊,但使用的列(x,y1ei1es1)來調用它們。 這怎麼辦?

頁84 gnuplot的手冊(記錄了using命令)的讀取:

Height Weight Age 
val1 val1 val1 
... ... ... 

然後將下面的情節命令都是等價

plot ’datafile’ using 3:1, ’’ using 3:2 

plot ’datafile’ using (column("Age")):(column(1)), \ 
’’ using (column("Age")):(column(2)) 

plot ’datafile’ using "Age":"Height", ’’ using "Age":"Weight" 

然而,當我想他們我只有行索引與他們自己。

+3

使用「x」:「y1」:「ei1」:「es1」與yerrorbars「在1:2:5:8中使用yerrorbars」和「plot」文件名「'plot」文件名「在此產生相同的結果。你運行的是哪個版本的Gnuplot,我在gnuplot 4.6 patchlevel 0中測試過。 – Thor 2012-07-16 10:01:50

+0

版本4.4 patchlevel 3隨Ubuntu一起提供。它太舊了嗎? – 2012-07-16 10:33:14

+1

是的,您需要4.6或更高版本才能使用此功能。 – Thor 2012-07-16 11:56:29

回答

5

快速瀏覽一下gnuplot 4.4 vs gnuplot 4.6(目前的穩定版本)的文檔,看來你嘗試使用的功能可能是在gnuplot 4.5中引入的(奇數是開發分支 - 當它們被認爲是穩定的,他們會增加到偶數)。可以想到的唯一方法就是用一些其他語言編寫一個簡單的腳本來返回列號(輸出到stdout)。下面是使用Python,雖然我敢肯定,你可以在awk做到這一點,如果你想繼續留在全POSIX環境一個簡單的例子:

#python indexing is 0 based, gnuplot datafile indexing 1 based 
COL_AGE=`python -c 'print(open("datafile").readline().split().index("AGE")+1)'` 
COL_HEIGHT=`python -c 'print(open("datafile").readline().split().index("HEIGHT")+1)'` 
plot "datafile" u COL_AGE:COL_HEIGHT 

這個小腳本不會做任何幻想(它假定列標題上,例如第一行),但使用python的力量,這將是很容易進一步擴展腳本:

#!/usr/bin/env python 
import sys 
with open(sys.argv[1]) as f 
    for line in f: 
     if (line.strip()): 
      print (line.split().index(sys.argv[2])+1) 
      sys.exit(0) 

現在你可以調用這個腳本:python script.py datafile AGE找出哪些列「年齡」已在。如果「年齡」不在任何列中,則爲錯誤。