68

I have a csv file which has 5 entries on every row. Every entry is whether a network packet is triggered or not. The last entry in every row is the size of packet. Every row = time elapsed in ms.

e.g. row

1 , 0 , 1 , 2 , 117

How do I plot a graph for e.g. where x axis is the row number and y is the value for e.g. 1st entry in every row?

Oli4
  • 489
  • 1
  • 9
  • 22

2 Answers2

96

This should get you started:

set datafile separator ","
plot 'infile' using 0:1
mgilson
  • 300,191
  • 65
  • 633
  • 696
Thor
  • 45,082
  • 11
  • 119
  • 130
  • @mgilson: "," is better yes, although "comma" is supported by Gnuplot 4.7. – Thor Feb 14 '13 at 13:54
  • 1
    So it is. Depending on the machine I'm working on, I usually use 4.6 or even 4.4 since 4.7 is still in development. But it's interesting that they added this. I still keep it around for reference or special purposes however :). I wonder if there are any subtle differences between `","` and `comma`. – mgilson Feb 14 '13 at 13:56
  • 1
    @mgilson: I don't think there is any difference, `src/set.c:2386` reads `df_separator = ',';` and `src/set.c:2392` reads `df_separator = sep[0];` which in my eyes should be the same. – Thor Feb 14 '13 at 14:15
  • looks the same to me too based on your comment. I've never really dug into the gnuplot source too much (maybe I should sometime). Is it well organized? – mgilson Feb 14 '13 at 14:17
  • 1
    @mgilson: Besides some whitespace annoyances it seems to be pretty readable, not that I've dug very far yet. I found the above lines by grepping for `"comma"` in `src/` :). – Thor Feb 14 '13 at 14:33
  • I jfst get invalid expression, doesn't seem to understand file paths for 'infile' – Joseph Garvin Apr 27 '20 at 16:33
  • @JosephGarvin: you should replace `infile` with your csv file – Thor Apr 27 '20 at 17:15
  • Thanks!, never realized this has to be done! – fahd Jan 12 '23 at 14:33
17

You can also plot to a png file using gnuplot (which is free):

terminal commands

gnuplot> set title '<title>'
gnuplot> set ylabel '<yLabel>'
gnuplot> set xlabel '<xLabel>'
gnuplot> set grid
gnuplot> set term png
gnuplot> set output '<Output file name>.png'
gnuplot> plot '<fromfile.csv>'

note: you always need to give the right extension (.png here) at set output

Then it is also possible that the ouput is not lines, because your data is not continues. To fix this simply change the 'plot' line to:

plot '<Fromfile.csv>' with line lt -1 lw 2

More line editing options (dashes and line color ect.) at: http://gnuplot.sourceforge.net/demo_canvas/dashcolor.html

  • gnuplot is available in most linux distros via the package manager (e.g. on an apt based distro, run apt-get install gnuplot)
  • gnuplot is available in windows via Cygwin
  • gnuplot is available on macOS via homebrew (run brew install gnuplot)
Max Desiatov
  • 5,087
  • 3
  • 48
  • 56
Oli4
  • 489
  • 1
  • 9
  • 22