tlre0952b
tlre0952b

Reputation: 751

GNUplot stdin, how to plot multiple lines?

OK so I am piping data into GNUplot in the form of, e.g:

2013-11-04 20:00:12,875,350,112,29,38,4,44,10,632,121

I have set the following in my Python code:

gnuplot.stdin.write("plot '-' u 1:2 t 'aa',\
'' u 1:3 t 'aa in',\
'' u 1:4 t 'bb',\
'' u 1:5 t 'bb in',\
'' u 1:6 t 'cc',\
'' u 1:7 t 'cc in',\
'' u 1:8 t 'dd',\
'' u 1:9 t 'dd in',\
'' u 1:10 t 'ee';\n")

However I keep getting errors such as:

gnuplot> plot '-' u 1:2 t 'aa',    '' u 1:3 t 'aa in',    '' u 1:4 t 'bb',    '' u 1:5 t 'bb in',    '' u 1:6 t 'cc',    '' u 1:7 t 'cc in',    '' u 1:8 t 'dd',    '' u 1:9 t 'dd in',    '' u 1:10 t 'ee';
                                                   ^
         line 1271: warning: Skipping data file with no valid points

Any ideas?

###UPDATE:###

Based on @Christoph's feedback; here's the code I'm currently using:

cur.execute(sql)
data = cur.fetchall()

c = 0            
for k in data:
    dataElement = data[c]    
    gnuplot.stdin.write("plot '-' u 1:2 t 'aa',\
    '' u 1:3 t 'aa in',\
    '' u 1:4 t 'bb',\
    '' u 1:5 t 'bb in',\
    '' u 1:6 t 'cc',\
    '' u 1:7 t 'cc in',\
    '' u 1:8 t 'dd',\
    '' u 1:9 t 'dd in',\
    '' u 1:10 t 'ee';\n")

    gnuplot.stdin.write("%s,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i\n" % dataElement[:])
    gnuplot.stdin.write("e\n")
    c = c + 1

Upvotes: 2

Views: 2135

Answers (3)

Dima
Dima

Reputation: 1

Pipe space-delimited data to this tool instead:

https://github.com/dkogan/feedgnuplot

It'll feed gnuplot thing the right commands, and make plots. Use --dump if you want to get a gnuplot script out of it.

Upvotes: 0

tlre0952b
tlre0952b

Reputation: 751

The following code solved it:

gnuplot.stdin.write("plot '-' u 1:2 t 'aa', " + \
            " '-' u 1:2 t 'aa in', " + \
            "'-' u 1:2 t 'bb', " + \
            "'-' u 1:2 t 'bb in', " + \
            "'-' u 1:2 t 'cc', " + \
            "'-' u 1:2 t 'cc in', " + \
            "'-' u 1:2 t 'dd', " + \
            "'-' u 1:2 t 'dd in', " + \
            "'-' u 1:2 t 'ee', " + \
            "'-' u 1:2 t 'ee in';\n")

for n in range(10):
    for dataElement in data:        
        a = n + 1
        if a == 11:
            break
        else:
            gnuplot.stdin.write("%s,%i\n" % (dataElement[0],dataElement[a]))
    gnuplot.stdin.write("e\n")
gnuplot.stdin.flush()

Upvotes: 1

Christoph
Christoph

Reputation: 48390

You must write an own data block for each '-'. The '' saves you only from retyping the file name, but doesn't reuse the data. Consider e.g. the following gnuplot script:

plot '-', '-'
2
4
6
e
10
12
14
e

Also make sure, that your delimiter is set properly: set datafile separator ','.

Here is a minimal python script, which plots two data sets from stdin, with the columns separated by comma:

#!/usr/bin/env python
import subprocess

gnuplot = subprocess.Popen(["gnuplot"], stdin=subprocess.PIPE)

gp_wrt = gnuplot.stdin.write

gp_wrt("set terminal pngcairo\n")
gp_wrt("set output 'test.png'\n")
gp_wrt("set datafile separator ','\n")
gp_wrt("plot '-' with lines title 'mytitle',\
'-' with lines title 'other title'\n")
for i in range(11):
    gp_wrt("{},{}\n".format(i, i**2))
gp_wrt("e\n")

for i in range(11):
    gp_wrt("{},{}\n".format(i, (0.5*i)**2))
gp_wrt("e\n")

So for your data it may look like

gnuplot.stdin.write("plot '-' u 1:2 t 'aa',\
    '' u 1:3 t 'aa in',\
    '' u 1:4 t 'bb',\
    '' u 1:5 t 'bb in',\
    '' u 1:6 t 'cc',\
    '' u 1:7 t 'cc in',\
    '' u 1:8 t 'dd',\
    '' u 1:9 t 'dd in',\
    '' u 1:10 t 'ee';\n")

for i in range(10):
    for dataElement in data:
        gnuplot.stdin.write("%s,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i\n" % dataElement[:])
    gnuplot.stdin.write("e\n")

But in that case you could simply use:

gnuplot.stdin.write("plot '-' u 1:2 t 'aa',\
    '' u 1:2 t 'aa in',\
    '' u 1:2 t 'bb',\
    '' u 1:2 t 'bb in',\
    '' u 1:2 t 'cc',\
    '' u 1:2 t 'cc in',\
    '' u 1:2 t 'dd',\
    '' u 1:2 t 'dd in',\
    '' u 1:2 t 'ee';\n")

for i in range(10):
    for dataElement in data:
        gnuplot.stdin.write("%s,%i\n" % (dataElement[0], dataElement[i])
    gnuplot.stdin.write("e\n")

i.e. each time you write only the relevant columns.

Upvotes: 3

Related Questions