4

Normally when using interactively gnuplot I do:

gnuplot> plot "state_log_6548032.data" using 4 with lines lt -1 lw 0.5 title "X axis" ,\
>"state_log_6548032.data" using 5 with lines lt 1 lw 0.5 title "Y axis" ,\
>"state_log_6548032.data" using 6 with lines lt 2 lw 0.5 title "Z axis"

However when I try to do the same from python using a subprocess:

gnuplot.write( "plot \"%s\" using 1 with lines lt -1 lw 0.5 title 'X axis' ,\ \n" %filename )
gnuplot.write( "plot \"%s\" using 2 with lines lt 1 lw 0.5 title 'Y axis' ,\ \n" %filename )
gnuplot.write( "plot \"%s\" using 3 with lines lt 2 lw 0.5 title 'Z axis' \n" %filename )

I get the following errors:

gnuplot> plot "state_log_6548032.data" using 1 with lines lt -1 lw 0.5 title 'X axis' ,\ 
                                                                                       ^
         line 0: invalid character \

gnuplot> plot "state_log_6548032.data" using 2 with lines lt 1 lw 0.5 title 'Y axis' ,\ 
                                                                                      ^
         line 0: invalid character \

I have spent a good amount of time trying to figure out if it is a problem with python, but I figured out it's an issue with gnuplot, which uses the escape character for some reason when called from console, but is not required in my case. However my issue remains. How can I plot the data above^^ in succesive lines either from the python subprocess by piping the instructions, or by creating a gnu file from python and calling the gnuplot subprocess to use that file ?

EDIT:

To anyone who might ever get stuck in this simple little thing: as explained below by the nice folk who keep this community alive, Python escapes the "\" when you use "\". So the solution was simply:

gnuplot.write( "plot \"%s\" using 1 with lines lt -1 lw 0.5 title 'X axis' ,\\\n" %filename )
        gnuplot.write( "\"%s\" using 2 with lines lt 1 lw 0.5 title 'Y axis' ,\\\n" %filename )
        gnuplot.write( "\"%s\" using 3 with lines lt 2 lw 0.5 title 'Z axis' \n" %filename )
2
  • have you tried the gnuplot module? Also, if you can I recommend using matplotlib for doing plotting. Commented Jul 27, 2011 at 2:03
  • @Keith I've got python-gnuplot-1.8 with debian, but I cannot find any documentation on how to use it. It seems unmaintained. I was unaware of matplotlib, thank you very much for the link, this seems more like what I was after in the first place ! Commented Jul 27, 2011 at 14:28

4 Answers 4

5

In the command-line gnuplot example you posted, you escape newlines to avoid having mile-long lines in your screen, but still send the data tu gnuplot as a single line.

This is cool if you're entering the data manually, but if you're doing programatically, why would you care? Just send a single line, Python won't complain about readability :). If you're curious as to what the problem was, it's because Python also uses \ as an escape sequence, so the character never reaches gnuplot.

That said, there are python-gnuplot libraries to handle all this dirty work for you. Check them out!

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

1 Comment

I do not really care how I do it, but this is the only way I know hot to send commands in gnuplot. agf told me in another question i posted that I could just make a tmp file and throw the commands there and feed them one by one to gnuplot. I've also thought of outputing all the commands in a gnu file and calling gnuplot to load that file. But for now I still can't get the above code to work.
1

I think when you do \ (backlash-space) in Python, you're escaping the space, and I think when you do do \ then hit enter in gnuplot you're escaping the newline.

Did you try it with just ,\n at the end, the python way to escape a newline? With just , or , (comma-space)? With just \n?

Probably, if you have to escape the newline, it's not really necessary, and just , will work to separate commands.

2 Comments

Hello agf. I've tried: ,\n and I get: line 0: function to plot expected. I've tried: \n and although it works, it only plots the first line. Finally I tried: ,\\\n and it worked :) Sorry to bother you with this. I will now try to automate it using a file as you suggested in my other question.
I tried escaping the space by a \ but they keep sending me back to earth.
1

To feed a literal backslash to gnuplot, use \\ in the Python string (the same way you use \n to feed it a literal newline). \ in general starts escape sequences, and in your case you have \; there is no special meaning for this, so it's just interpreted as a space (i.e. the backslash is dropped out).

You can test the contents of your strings by printing them rather than passing them to gnuplot.write.

That said, chances are good you can just put the whole thing on one line, instead of feeding the backslashes and newlines to gnuplot.

1 Comment

I've gotten python-gnuplot-1.8 but I cannot find any documentation on how to use it !
1

In my case of Gnuplot 4.6 and opensuse linux the type and order of enclosing apostrophes was important: with gnuplot started as subprocess.Popen, writing:

gnuplot.stdin.write("set format x '%d %h\\\n%H:%M'")

... printed newline character as literal in single-line label, when:

gnuplot.stdin.write('set format x "%d %h\\\n%H:%M"')

... works as expected, producing two-lines labels along the time x axis.

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.