2012-08-25 49 views
4

我已数据集(文件名“数据”)是这样的:
a 10.1
b 10.1
c 10.2
b 15.56
a 3.20

,我希望绘制该数据为点。当我尝试:
plot 'data' using 2:xticlabels(1)
我得到了5个x轴值a,b,c,b,a的情节,但我只想得到3个(a,b,c(顺序不重要))的情节与所有5 y值。可能吗?gnuplot的,非数字重复x值

我真正的数据文件看起来像这样:
2-8-16-17-18 962.623408
2-3-4-5-6 -97.527840
2-8-9-10-11 962.623408
2-8-9-10-11 937.101308
2-3-4-5-6 37.101308

,拥有大约一千记录。


我不知道如何使用mgilson的代码,但他给了我一个主意。我添加到数据文件附加列(索引):

1 a 10.1 
2 b 10.1 
3 c 10.2 
2 b 15.56 
1 a 3.20
之后,在gnuplot的ploting很简单:
plot 'data' u 1:3

我用perl的,所以我的脚本lookls这样的:

#!/usr/bin/perl 
$index_number = 0; 
while (<>) 
{ 
    $line = $_; 
    @columns = split(" ",$line); 
    $col1 = $columns[0]; 
    $col2 = $columns[1]; 
    if(not exists $non_numeric{$col1}) 
    { 
     $index_number++; 
     $non_numeric{$col1} = $index_number; 
    } 
    print "".$non_numeric{$col1}."\t".$col1."\t".$col2."\n"; 
} 
+1

x轴上的顺序实际上是否重要? – mgilson

回答

1

我怀疑你可以想出一个仅限gnuplot的解决方案。但是,只要你的系统上安装了python2.5或更新的版本,这个就应该工作。 (它与你的测试数据一起工作)。

import sys 
import collections 

data = collections.defaultdict(list) 
keys = [] 

# build a mapping which maps values to xticlabels (hereafter "keys") 
# Keep a second keys list so we can figure out the order we put things into 
# the mapping (dict) 
with open(sys.argv[1]) as f: 
    for line in f: 
     key,value = line.split() 
     data[key.strip()].append(value) 
     keys.append(key.strip()) 

def unique(seq): 
    """ 
    Simple function to make a sequence unique while preserving order. 
    Returns a list 
    """ 
    seen = set() 
    seen_add = seen.add 
    return [ x for x in seq if x not in seen and not seen_add(x) ] 

keys = unique(keys) #make keys unique 

#write the keys alongside 1 element from the corresponding list. 
for k in keys: 
    sys.stdout.write('%s %s\n' % (k, data[k].pop())) 

# Two blank lines tells gnuplot the following is another dataset 
sys.stdout.write('\n\n') 

# Write the remaining data lists in order assigning x-values 
# for each list (starting at 0 and incrementing every time we get 
# a new key) 
for i,k in enumerate(keys): 
    v = data[k] 
    for item in v: 
     sys.stdout.write('%d %s\n' % (i, item)) 

现在脚本绘制这样的:

set style line 1 lt 1 pt 1 
plot '<python pythonscript.py data' i 0 u 2:xticlabels(1) ls 1,\ 
    '' i 1 u 1:2 ls 1 notitle 

这里是如何工作的。当您执行类似plot ... u 2:xticlabels(1)的操作时,gnuplot 隐式地将分配连续的整数x值给数据点(从0开始)。 python脚本重新安排数据以利用这个事实。基本上,我创建了一个映射,将第一列中的“键”映射到与该键对应的元素列表。换句话说,在您的虚拟数据文件中,密钥'a'映射到值列表[10.1, 3.2]。但是,python字典(映射)不是有序的。所以我保留第二个维护顺序的列表(例如,让你的轴被标记为'a','b','c'而不是'c','a','b')。我确定轴列表是唯一的,以便我可以使用它来打印必要的数据。我以2遍的方式写入数据。第一遍只打印每个列表中的一个值以及映射“key”。第二遍打印剩下的值以及gnuplot将隐含分配给它们的x值。在两个数据集之间,我插入2个空白行,以便gnuplot可以使用index关键字(此处缩写为i)来区分差异。现在我们只需要相应地绘制两个数据集。首先,我们设置一个线条样式,以便在绘制时两个样式都具有相同的样式。然后我们绘制索引0(第一个数据集)的xticlabels和索引1使用x值,y值对计算出的python脚本(u 1:2)。对不起,解释很长(并且原始版本稍微有些bug)。祝你好运,快乐gnuplotting!