Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

gnuplot - 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

That is: date series count.

Note that the series are not in a consistent order on each day (and might not appear at all).

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).

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.

How do I do this?

Note: I'm using gnuplot 4.4

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 "
";}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

and then plot this multi-dataset file with

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)

which produces enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...