Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gnuplot sending newline and ",\" from python

Tags:

python

gnuplot

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 )
like image 368
Ælex Avatar asked Feb 24 '23 11:02

Ælex


2 Answers

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!

like image 175
slezica Avatar answered Feb 26 '23 00:02

slezica


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.

like image 40
agf Avatar answered Feb 25 '23 23:02

agf