Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...