93

I am trying to plot a graph using gnuplot. I have six text files. Each text file contains two columns. The first column represents time in seconds (a floating point number). The second one is a sequence number. I want to plot the graph of time vs. sequence number in a single graph for all six files. I am using this file to do that.

set terminal png
set output 'akamai.png'

set xdata time
set timefmt "%S"
set xlabel "time"

set autoscale

set ylabel "highest seq number"
set format y "%s"

set title "seq number over time"
set key reverse Left outside
set grid

set style data linespoints

plot "print_1012720" using 1:2 title "Flow 1", \
plot "print_1058167" using 1:2 title "Flow 2", \
plot "print_193548"  using 1:2 title "Flow 3", \ 
plot "print_401125"  using 1:2 title "Flow 4", \
plot "print_401275"  using 1:2 title "Flow 5", \
plot "print_401276"  using 1:2 title "Flow 6"

Where my files are:

  • print_1012720
  • print_1058167
  • print_193548
  • print_401125
  • print_401275
  • print_401276

It is giving a strange error as below:

"plot.plt", line 24: undefined variable: plot

Am I doing something wrong? Is it possible to plot the input data from different files in the same graph?

rainer
  • 3,295
  • 5
  • 34
  • 50
liv2hak
  • 14,472
  • 53
  • 157
  • 270

3 Answers3

145

You're so close!

Change

plot "print_1012720" using 1:2 title "Flow 1", \
plot "print_1058167" using 1:2 title "Flow 2", \
plot "print_193548"  using 1:2 title "Flow 3", \ 
plot "print_401125"  using 1:2 title "Flow 4", \
plot "print_401275"  using 1:2 title "Flow 5", \
plot "print_401276"  using 1:2 title "Flow 6"

to

plot "print_1012720" using 1:2 title "Flow 1", \
     "print_1058167" using 1:2 title "Flow 2", \
     "print_193548"  using 1:2 title "Flow 3", \ 
     "print_401125"  using 1:2 title "Flow 4", \
     "print_401275"  using 1:2 title "Flow 5", \
     "print_401276"  using 1:2 title "Flow 6"

The error arises because gnuplot is trying to interpret the word "plot" as the filename to plot, but you haven't assigned any strings to a variable named "plot" (which is good – that would be super confusing).

Community
  • 1
  • 1
mgilson
  • 300,191
  • 65
  • 633
  • 696
81

You may find that gnuplot's for loops are useful in this case, if you adjust your filenames or graph titles appropriately.

e.g.

filenames = "first second third fourth fifth"
plot for [file in filenames] file."dat" using 1:2 with lines

and

filename(n) = sprintf("file_%d", n)
plot for [i=1:10] filename(i) using 1:2 with lines
Richard
  • 56,349
  • 34
  • 180
  • 251
  • 6
    I know this is old, but thank you for adding that alternative solution. Wasn't aware of loops in gnuplot, and they're an amazing feature. – Amit Apr 10 '14 at 13:37
  • Assuming normal filename convention (name.dat) I think this should be file.".dat". The first . concatenates the filename to "dat" but isn't included in the filename actually used in the plotting command. – jake Oct 28 '14 at 02:10
  • How can I plot *all* the files in a directory without explicitly writing their names? (http://stackoverflow.com/q/29969393/855050) – a06e Apr 30 '15 at 13:43
  • I've added an answer to that question, @becko. – Richard Apr 30 '15 at 17:47
31

replot

This is another way to get multiple plots at once:

plot file1.data
replot file2.data
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • 2
    best answer, the first one did not work. Output was: "format must have 1-7 conversions of type double (%lf)". While the other options only allow you to do it if all the data columns are the same. – RSM Jul 21 '20 at 00:55