是否可以将索引用作直方图中的Xtic值?

时间:2018-10-05 22:54:53

标签: indexing gnuplot histogram

在具有以下结构的数据的文件中:

# [year]
"Town A" population
"Town B" population
"Town C" population

# [year2]
"Town A" population
"Town B" population
"Town C" population

# [year3]
"Town A" population
"Town B" population
"Town C" population

我设法使用以下方法创建了直方图:

set style data histogram
set style histogram columnstacked
p 'file.dat' index '[year]' u 2:key(1) ,\
'' index '[year2]' u 2,\
'' index '[year3]' u 2;

以前的设置几乎按照我需要的方式生成图形,但是,我希望能够将索引名称(标签?)用作Xtic值,目前,Xtic值为0、1,和2代替year,year2和year3。 gnuplot可以将索引值用作具有这种数据结构的直方图中的xtics吗?

2 个答案:

答案 0 :(得分:1)

file.dat中的数据块之间需要两个空白行:

# [year]
"Town A" 123
"Town B" 543
"Town C" 789


# [year2]
"Town A" 234
"Town B" 666
"Town C" 1000


# [year3]
"Town A" 345
"Town B" 600
"Town C" 800

Gnuplot命令(需要版本5.2.5或更高版本)

set style data histogram
set style histogram columnstacked
set style fill solid border lc black
plot 'file.dat' index '[year]' u 2:key(1) title strcol(-2),\
     '' index '[year2]' u 2 title strcol(-2),\
     '' index '[year3]' u 2 title strcol(-2)

请注意,列-2是指索引值。那不是什么新鲜事,但是现在版本5.2.5允许您在strcol()函数(stringcolumn()的缩写)中使用它。标题和列间距可以改善。 enter image description here

答案 1 :(得分:0)

我看不到在plot命令本身中进行设置的方法。这是一个好主意,我将其作为gnuplot的功能要求进行推广。我可以建议的最好方法是,假设您事先知道索引字符串集。 我将其显示为对N的迭代,因为我假设您可能确实有3个以上,而对于较大的数字,该迭代的键入要少得多。

N = 3
array INDEX[N] = [ "[year1]", "[year2]", "[year3]" ]
set xtics 1
set for [i=1:N] xtics add (INDEX[i] i-1)

plot 'file.dat' index INDEX[1] u 2:key(1) ,\
     for [i=2:N] '' index INDEX[i] u 2