Steve
Steve

Reputation: 8293

gnuplot stdin, how to plot two lines?

I'm trying to produce a plot with two lines using data taken from stdin. I have a file "test.csv":

0,1.1,2
1,2,3
2,6,4
4,4.6,5
5,5,6

I've been trying to plot this with commands like,

$ cat test | gnuplot -p -e "set datafile separator \",\"; plot '-' using 1:2 with lines, '' using 1:3 with lines;"

But no matter what I try I get,

line 5: warning: Skipping data file with no valid points

I assume this is because for the second line, stdin has already been exhausted. Is there a way to get gnuplot to take data from each column of stdin for different plots?

Thanks.

Upvotes: 31

Views: 34282

Answers (9)

robotik
robotik

Reputation: 17

Maybe this is an old question but I've found an interesting solution based on the other answers:

cat filename | awk -- '{print $0} END{print "e"}' | tee -i -a - -

The output would be:

"contents of filename"
e
"contents of filename"
e
"contents of filename"
e

Now cat and awk work as usual, the utility tee on the other hand allows us to copy the standard output piped from the previous command to a file, but this file can be stdin itself so we can make many copies of it, by specifying n times - after -a.

The option -i stops it being interrupted by signals during the copy, -a tells it to append the input to standard input without overwriting and then write the whole thing to stdout.

For reference take a look at Tee Utility

Upvotes: 1

LEo
LEo

Reputation: 487

I had to do the following, since adding '-' did not work for me:

 cat filename | awk -- '{print $0} END{print "e"}' | tee -i -a /dev/stdout /dev/stdout 

Upvotes: 1

mlg
mlg

Reputation: 1511

Have you tried chart? Your case would be as easy as:

cat test | chart line ','

And would give you:

line chart

Upvotes: 2

PonyEars
PonyEars

Reputation: 2254

The "-" is used to specify that the data follows the plot command. So if you use it, you'll need to do something like:

echo "set datafile separator \",\"; plot '-' using 1:2 with lines, '' using 1:3 with lines;" | cat - datafile.dat | gnuplot -p

(Quoting above probably needs to be escaped).

What're you looking for is this:

plot '< cat -'

Now, you can do:

cat test | sed ... | gnuplot -p "plot '< cat -' using ..."

Note that you might need to feed in the input data via stdin multiple times if you're using options with plot, like so:

cat testfile testfile | gnuplot -p "plot '< cat -' using 1, '' using 2"

In the above case, testfile must end with a line that has the sole character 'e' in it.

Manual reference

Upvotes: 21

Christoph
Christoph

Reputation: 48390

Gnuplot can read from stdin, but for each plot statement, a new data set is required. The following works fine:

cat test.csv | gnuplot -p -e "set datafile separator ','; plot '-' using 1:2 w l"

The error appears as soon as you append the second plot command with , '' using 1:3. For this you need to send the data again as the first data set isn't stored interally for reuse. So for you two plot commands, the following snippet works fine:

echo 'e' | cat test.csv - test.csv | gnuplot -p -e "set datafile separator ','; plot '-' using 1:2 w l, '' using 1:3 w l"

That writes the data file twice, separated by an e which indicates the end of the data for the first plot command.

Upvotes: 12

Gordon Childs
Gordon Childs

Reputation: 36072

gnuplot seems to need random access (i.e. not stdin), so I think you're stuck with

# explicitly open "test" file
$ gnuplot -p -e "set datafile separator \",\"; plot 'test' using 1:2 with lines, '' using 1:3 with lines;"

Upvotes: 0

bdew
bdew

Reputation: 1330

I managed to work around this by sending the data twice, terminated by am "e" on it's own line after each block. So your input should look like

set datafile separator ","; plot '-' using 1:2 with lines, '' using 1:3 with lines
0,1.1,2
1,2,3
2,6,4
4,4.6,5
5,5,6
e
0,1.1,2
1,2,3
2,6,4
4,4.6,5
5,5,6
e

Upvotes: 12

Martin
Martin

Reputation: 1114

For me this works when I do:

set datafile separator ','
plot "test.csv" using 1:2 with lines

it seems that you used "," for datafile separator instead of ','

Upvotes: 0

arnsholt
arnsholt

Reputation: 871

I'd try converting the csv file to space separated (assuming no of the records span multiple lines) by piping it through sed instead of setting the separator:

cat test | sed 's/,/ /g' | gnuplot -p -e "plot '-' using ..."

Upvotes: 2

Related Questions