0

I have been trying to solve the problem for more than a day, but whatever api (C++, python) and gnuplot version would not be, the problem is the same. Let's look at several different examples of python code that works fine without an API in the gnuplot terminal.

1.From here

from subprocess import Popen, PIPE
gnuplot = 'gnuplot'
p = Popen([gnuplot, '-persist'], stdin=PIPE)
cmd = b"""set terminal qt
set timefmt "%H:%M:%S"
set xdata time
set term png size 1600, 1200
set output "test.png"
set yrange [-1.5:1.5]
set grid
set title ("test_param"); 
plot "test.dat" using 1:2 with lines lt rgb "blue" lw 2 title "Q value"
"""
p.stdin.write(cmd); p.stdin.flush()

2.From here

import PyGnuplot as pg

pg.c("set terminal qt")
pg.c('set timefmt "%H:%M:%S";')
pg.c('set xdata time;')
pg.c('set yrange [-1.5:1.5]')
pg.c('set grid')
pg.c('plot "test.dat" using 1:2 with lines lt rgb "blue" lw 2 title "Q value"')
pg.c('set term png size 1600, 1200;')
pg.c('set output "test.png"')

In both cases i get some strange error:

gnuplot> t output "test.png"
         ^
         line 0: invalid command

I have tried almost all API examples and nothing works with almost the same errors

Example of "test.dat":

00:00:05.742    -0.098281
00:00:05.745    -0.098281
00:00:05.746    -0.099969
00:00:05.747    -0.100051
00:00:05.748    -0.100051
00:00:05.749    -0.100037
00:00:05.750    -0.102142
00:00:05.750    -0.102150
00:00:05.752    -0.101780
Vernadsky
  • 3
  • 3

1 Answers1

1

Although I haven't fully understood all the details, the following works for me (Python 3.6.3 Windows 10).

Some difference to your first code example:

  • Why do you set term qt if you set it to png directly afterwards?
  • in my case, I had to give the full path for the gnuplot executable (because gnuplot path is not set in the Windows environment variables). Mind the r'...' in the Python code
  • I used absolute paths for the data and the plot (otherwise it depends on where you start your Python script) and I guess for the file paths you have to use single quotes (for gnuplot?!) and escape the backslashes \\ (for Python?!). Alternatively, you could use only single foreslashs /, e.g. set output 'C:/Users/W2102029/Scripts/test.png'

If there are better ways and explanations, I am happy to learn from more experienced Windows/Python/gnuplot users.

Code:

from subprocess import Popen, PIPE

gnuplot = r'C:\Users\Test\Programs\gnuplot5.4.0\bin\gnuplot'

p = Popen([gnuplot, '-persist'], stdin=PIPE)
cmd = b"""
set xdata time
set timefmt "%H:%M:%S"
set term png size 1600, 1200
set output 'C:\\Users\\Test\\Scripts\\test.png'
set yrange [-1.5:1.5]
set grid
set title ("test_param") noenhanced
plot 'C:\\Users\\Test\\Scripts\\test.dat' using 1:2 with lines lt rgb "blue" lw 2 title "Q value"
set term qt
replot
"""
p.stdin.write(cmd)
p.stdin.flush()

Result:

  • a file test.png with the plot will be created on disk
  • a window with qt terminal opens and the plot appears and persists.
theozh
  • 22,244
  • 5
  • 28
  • 72