从同一文件中绘制多个系列 [英] Plot multiple series from the same file
问题描述
我有一个类似于以下内容的数据文件:
I've got a data file that looks something like the following:
2016-08-16 dogs 12
2016-08-16 cats 9
2016-08-17 cats 4
2016-08-17 mice 42
2016-08-17 dogs 6
即:date series count
.
请注意,该系列并非每天都有一致的顺序(并且可能根本不会出现).
Note that the series are not in a consistent order on each day (and might not appear at all).
此外,我的输入文件也没有按顺序排列日期(尽管如果我必须可以用sort
进行修复),并且某些日期可能会丢失(因为当天没有数据点.
Further, my input file doesn't have the dates in order either (though I can fix that with sort
if I have to), and some of the dates might be missing (because there are no data points for that day).
我想使用 gnuplot 进行渲染. X轴应显示日期; Y轴应显示计数;每个系列应该是自己的一行,并在图例中带有标签.
I'd like to use gnuplot to render this. The X-axis should display the date; the Y-axis should display the count; each series should be its own line, with labels in the legend.
我知道如何从一个单独的文件中渲染每个系列,但是该系列不是固定的,因此我正在寻找不需要该系列的东西.
I know how to render each series from a separate file, but the series are not fixed, so I'm looking for something that doesn't require that.
我该怎么做?
注意:我正在使用gnuplot 4.4
推荐答案
也许首先可以这样转换输入文件(input.txt
)文件:
perhaps one could first transform the input file (input.txt
) file like so:
sort -k2,2 -k1,1 input.txt | \
gawk '{if($2!=prev){if(NR>1){print "\n";}print "date", $2; prev=$2;}print $1,$3}' > input2.txt
在这种情况下提供的
which in this particular case provides
date cats
2016-08-16 9
2016-08-17 4
date dogs
2016-08-16 12
2016-08-17 6
date mice
2016-08-17 42
,然后使用
datafile="input2.txt"
stats datafile
set xdata time
set timefmt '%Y-%m-%d'
N=int(STATS_blocks)-1
plot for [i=0:N] datafile index i using 1:2 w lp t columnhead(2)
产生
这篇关于从同一文件中绘制多个系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!