gnuplot:按字符串值堆叠数据

时间:2014-07-16 14:18:18

标签: gnuplot

我有这个输入文件(它是自动生成的):

"John";"580"
"Monica";"580"
"Monica";"580"
"John";"580"
"John";"580"
"John";"580"
"John";"580"
"Mark";"576"
"Monica";"580"
"John";"580"
"John";"580"
"Mark";"586"
"John";"580"
"John";"580"
"John";"580"
"John";"580"
"Monica";"580"

我想创建一个图表,其中包含每人第二列数的总和。

我尝试了不同的可能解决方案无济于事。例如:

set datafile separator ";"    
plot "input.csv" u  1:2:xtic(1) smooth cumulative w boxes

这会返回一个错误,我知道gnuplot期待一个数值而不是一个字符串,所以我该怎么办呢?

我无法在文件中添加任何其他列,因为它是自动生成的。

感谢。

1 个答案:

答案 0 :(得分:0)

这有点棘手。基本上,您必须为每个字符串分配一个唯一的数字,用于添加属于相同字符串的值:

list = ''
index(w) = words(substr(list, 0, strstrt(list, w)-1))
add_label(d) = (strstrt(list, d) == 0 ? list=list.' '.d : '')

set datafile separator ";"    

plot 'input.csv' using (d='|'.strcol(1).'|', add_label(d),index(d)):2:xtic(1) smooth frequency w boxes

请注意,您必须使用smooth frequency汇总属于单个字符串的所有值,cumulative会将所有值相加。

我提出的解决方案仅适用于没有空格的字符串(无论它们是否在引号中),但可能会扩展为使用空格(可能会变得有点冗长)。

您将在之前的回答Gnuplot, plotting a graph with text on y axis中找到更详细的描述。