benschneider / PyGnuplot

Python interface to gnuplot
MIT License
45 stars 18 forks source link

PyGnuplot on windows doesnt work out of the box #16

Closed irr-github closed 2 years ago

irr-github commented 3 years ago

There's a small issue, which is that "gnuplot" isn't a name that works in cmd and popen can't find it. I would suggest having a "gnuplot_adress" at top to make it easier to change. The below works for me on win10

from subprocess import Popen as _Popen, PIPE as _PIPE

default_term = 'wxt' # change this if you use a different terminal gnuplot_address = r"C:\Program Files\gnuplot\bin\gnuplot.exe"

class _FigureList(object):

def __init__(self):
    proc = _Popen([gnuplot_address, '-p'], shell=False, stdin=_PIPE, universal_newlines=True)  # persitant -p
    self.instance = {0 : [proc, default_term]}  # {figure number : [process, terminal type]}
    self.n = 0  # currently selected Figure
    # Format:
    # instance[self.n][0] = process
    # instance[self.n][1] = terminal

def figure(number=None): '''Make Gnuplot plot in a new Window or update a defined one figure(num=None, term='x11'):

figure(2) # would create or update figure 2 figure() # simply creates a new figure returns the new figure number ''' if not isinstance(number, int): # create new figure if no number was given number = max(fl.instance) + 1

if number not in fl.instance:  # number is new
    proc = _Popen([gnuplot_address, '-p'], shell=False, stdin=_PIPE, universal_newlines=True)
    fl.instance[number] = [proc, default_term]

fl.n = number
c('set term ' + str(fl.instance[fl.n][1]) + ' ' + str(fl.n))
return number
benschneider commented 3 years ago

indeed that's a good solution. I might add it to the experimental branch and update it along with other fixes.

benschneider commented 2 years ago

new gnuplot_directory option is now implemented in the new version;

i.e. example usage should work now:

from PyGnuplot import gp Figure1 = gp(r"C:\Program Files\gnuplot\bin\gnuplot.exe") Figure1.a("plot sin(x)")