2

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

3
  • Does it work without the titles? Commented Nov 22, 2013 at 7:16
  • @Bernhard No. I get the same errors as above, i.e.: gnuplot> plot '-' u 1:2, '' u 1:3, '' u 1:4, '' u 1:5, '' u 1:6, '' u 1:7, '' u 1:8, '' u 1:9, '' u 1:10; Commented Nov 22, 2013 at 15:02
  • @Christoph I have asked a more specific question with more focus as suggested (by you). Commented Nov 22, 2013 at 15:34

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

11 Comments

I have put gnuplot.stdin.write("e\n") after each time the data is sent and I am getting somewhere with this. All the relevant data points are now showing up with respective legends. However, I am a little stuck; the first row have been accepted but none of the following rows of data are. I do not follow your plot '-', '-' part?
That was meant as a pure gnuplot script and should show, that for two '-' parts you need to send the data twice, each time terminated with an e. I added a minimal python script, which plots two data sets coming from stdin.
I have 440 lines to submit through stdin to gnuplot. Are you telling me I need 439 '-', after plot ending with '-'; and then looping through the data to be submitted?
With 'lines' do you mean a line in the plot (severy connected points) or a row in the data set? And if you indeed have 440 lines to plot, then you need as many '-', but I can't believe that, especially with all of them having keys :) If you have 440 rows in a data set, which are connected together to a single line in the plot, then you need only a single '-'.
I have included the code in my original question; that's what I think you mean. Is it?
|
1

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()

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.