4

I have the following data, which I wan't to plot using GNUPLOT:

#TIME #VALUE #SOURCE
1 100 A
1 88 B
2 115 A
2 100 B
3 130 A
3 210 B

I want to have two lines drawn, depending on the value of column #SOURCE. One line for A and one line for B. Is this possible with GNUPLOT and if yes how?

Is it possible to also draw a summation of column #VALUE depending over column #TIME? Means, that for all equal entries in #TIME, the values in #VALUE will be summed up.

Thanks in advance, Frank

Aaginor
  • 4,516
  • 11
  • 51
  • 75
  • Possible duplicate of [How to plot single/multiple lines depending on values in a column with GNUPlot](http://stackoverflow.com/questions/24325128/how-to-plot-single-multiple-lines-depending-on-values-in-a-column-with-gnuplot) – Ciro Santilli OurBigBook.com May 02 '16 at 18:31

1 Answers1

2

One way to do it would be to use grep to locate lines ending with A or B and plot the result. You can do this in a single plot line with a for loop if you know the characters lines will end in:

plot for [s in 'A B'] sprintf("<(grep -v '%s$' data.dat)", s) u 1:2 w l

This plots the data you provided (saved in data.dat) as two different lines.

You could also change the for part to [s in 'word1 word2 word3'] or any other string you like. If you don't know the character/word lines will be ending with you would probably need to pass the file twice first to determine the string for the for loop and a second time to do the plotting.

ilent2
  • 5,171
  • 3
  • 21
  • 30
  • Thanks for your answer! As I don't know the names of the source, I adapted the routine that writes the datafile. Now I simply have a column for each source (ValueA, ValueB, ...) and plot it with [plot 'file.csv' u 2:1 title 'A', 'file.csv' u 3:1 title 'B', ... – Aaginor Apr 23 '15 at 12:17
  • See also [How to use one of the column of data as legend in gnuplot?](http://stackoverflow.com/a/29832030/2604213) for an example how to extract the unique values of a column. – Christoph Apr 23 '15 at 18:56
  • plot for [s in 'a b c d e f g'] sprintf("<(grep -v '%s$' testdata1.dat)", s) u 1:3 w l ..... I am getting error: Invalid expression at s. The version is 4.0...why?? – GP92 Apr 29 '15 at 14:47
  • I have tested the same in windows with 5.0 version but there I am receiving error: can not pipe data....is the piping syntax is different in windows??? – GP92 Apr 29 '15 at 14:48
  • I'm not sure if this will work on Windows without `bash` and `grep` installed (and running `gnuplot` from `bash` with `grep` in the `PATH`). There might be some other syntax for pipes on Windows but I'm not sure. – ilent2 Apr 29 '15 at 22:49
  • As for the version of `gnuplot` I'm using: Version 4.6 patchlevel 5 (Linux x86_64). I think this particular `for` syntax was introduced in 4.4+. – ilent2 Apr 29 '15 at 22:50