1

(I wouldn't be suprised if this is a duplicate, I can't find the right search term to find it though)

I have a data file of the form (simplified a bit):

date       time       label      value
2013-03-17 10:09:28   thing 1    54
2013-03-17 10:09:32   thing 2    20
2013-03-17 10:10:02   thing 3    35
2013-03-17 10:10:03   thing 1    59
2013-03-17 10:10:21   thing 1    64
2013-03-17 10:10:37   thing 3    37
2013-03-17 10:10:37   thing 2    40
2013-03-17 10:10:42   thing 3    41
2013-03-17 10:10:44   thing 2    59

...and what I want to do is create a multi-line plot, each of 'thing 1', 'thing 2' and 'thing 3', and have Gnuplot automatically label them accordingly.

If I split the file into separate datasets, one for each label, then I know I can do a multi-line plot like this:

set timefmt '%Y-%m-%d %H:%M:%S';
set xdata time;    
plot 'thing1.txt' using 1:3 title 'thing 1', \
     'thing2.txt' using 1:3 title 'thing 2'

...however this seems to be overkill. Can this be achieved with Gnuplot without having to split the file into separate files-per-dataset? In reality there's about 200 separate labels, so if Gnuplot can automate the labelling it will save a lot of manual pre-processing of the data.

Chris J
  • 30,688
  • 6
  • 69
  • 111
  • You really want 200 labels in your figure? – Bernhard Apr 03 '13 at 15:29
  • Initially yes. Ultimately I will be filtering it down so I'm only managing a handful (possibly less than 10) but I don't know what those are without doing some processing. I was hoping that visulising the data would save me writing a script to do the initial number crunching. Ultimately I'm looking for a trend, and want to know if that trend is common to everything or just a select few items so I know how next to proceed with the data. The input file is 400,000 lines so it's not something I can inspect by eye. – Chris J Apr 03 '13 at 15:44
  • Did you consider writing a kind of wrapper script that generates the gnuplot-script and execute it? – Bernhard Apr 03 '13 at 17:11

3 Answers3

0

Yeah, you can do something like this:

plot for [i=1:300] 'thing.txt' using 1:(($4 == i)? $3:NaN) title 'thing '.i

This won't be the most efficient (I'm pretty sure gnuplot'll read your file 300 times), but it'll get the job done.

mgilson
  • 300,191
  • 65
  • 633
  • 696
0

Although the @mgilson's answer will work, it could be very slow if the data is big since it reads the file 300 times.

I think it is more efficient to put the labels by hand. If the labels are numbers, we can use them to select a color. For instance, the following lines will plot column 5 over column 1, where the color is selected from column 4, and the labels are placed by plotting an imaginary line with constant value NaN (1/0)

plot 'thing.txt' u 1:5:4 pt 7 lc variable notitle,\
     for [i=1:300] 1/0 w p pt 7 lc i title 'thing '.i

Notice that, by default, the gnuplot colors are cyclic, i.e each N lines the colors will be repeated (typically, N=9). You would need to set each of the 300 cases by hand if you need different colors for them. To do this automatically, this question in SO offers some good approaches.

Community
  • 1
  • 1
vagoberto
  • 2,372
  • 20
  • 30
0

Yes, there are similar questions, but later than yours. So, actually, those should be "duplicates" of your question. You have to filter your data as @mgilson suggested.

However, the existing answers might not be ideal, because @mgilson's solution will read the file many times and both @vagoberto's and @mgilson's solution will only plot with disconnected points. I assume you want to plot connected lines either via with lines or with linespoints.

Data: SO15790026.dat

date       time       label      value
2013-03-17 10:09:28   thing 1    54
2013-03-17 10:09:32   thing 2    20
2013-03-17 10:10:02   thing 3    35
2013-03-17 10:10:03   thing 1    59
2013-03-17 10:10:21   thing 1    64
2013-03-17 10:10:37   thing 3    37
2013-03-17 10:10:37   thing 2    40
2013-03-17 10:10:42   thing 3    41
2013-03-17 10:10:44   thing 2    59

Script 1: (works for gnuplot>=4.6.0, March 2012)

The "trick" to "bridge" missing data for gnuplot>=4.6.0 is from here.

### filter data, gnuplot>=4.6.0
reset

FILE = "SO15790026.dat"

set xdata time
set timefmt "%Y-%m-%d %H:%M:%S"
set format x "%H:%M:%S"
set grid x
set grid y
set key top left

plot for [i=1:3] x0=y0=NaN FILE u (column(4)==i?(y0=$5,x0=timecolumn(1)):x0):(y0) w lp pt 7 lc i ti sprintf("thing %d",i)
### end of script

Script 2: (works for gnuplot>=5.0.7, Aug. 2017)

### filter data, gnuplot>=5.0.7
reset session

FILE = "SO15790026.dat"

myFilter(colD,colF,i) = column(colF)==i ? column(colD) : NaN

myTimeFmt = "%Y-%m-%d %H:%M:%S"
set format x "%H:%M:%S" timedate
set datafile missing NaN
set grid x,y
set key top left

plot for [i=1:3] FILE u (timecolumn(1,myTimeFmt)):(myFilter(5,4,i)) w lp pt 7 lc i ti sprintf("thing %d",i)
### end of script

Result: (created with gnuplot 4.6.0)

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72