benschneider / PyGnuplot

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

Figure handling - a suggestion for improvement #2

Open steview2000 opened 7 years ago

steview2000 commented 7 years ago

First of all, thanks for the library. That is pretty much what I was looking for - a simple interface to gnuplot.

However, I was a bit unsatisfied with the figure handling. After I had plotted multiple figures and I wanted to zoom into one of them, after zooming out it replotted the last figure instead of the active figure. For example, after plotting data in 3 different figures, I want to zoom into figure(2). When I want to zoom out again, I hit "a", but instead of zooming out, it replotted figure(3). I checked and this is indeed a problem with Gnuplot, and not with your script. Also, all changes with PyGnuplot.c() are valid for all figures later on, which is sometimes annoying.

Therefore, I have updated your script so that for each figure a new Gnuplot-subprocess is started. Now, PyGnuplot.figure() switches between completely independent Gnuplot sessions.

The parts that I have changed:

class _emptyClass(object):
def __init__(self):
self.figNum = [1] self.figNum[0] = 1 self.proc = [1] self.proc[0] = _Popen(['gnuplot', '-p'], shell=False, stdin=_PIPE) # persitant -p `_vc = _emptyClass()` `proc = _vc.proc[0]` def figure(number=None, term='x11'): '''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 ''' global proc ` if not isinstance(number, int): number = _vc.figNum[-1]+1 if number not in _vc.figNum: _vc.proc.append(_Popen(['gnuplot', '-p'], shell=False, stdin=_PIPE)) # persitant -p _vc.figNum.append(number)`

_vc.figNum.sort()
proc = _vc.proc[_vc.figNum.index(number)]
c('set term '+str(term)+' '+str(number))
return number

`

benschneider commented 7 years ago

Thanks, this is quite useful will implement it into the next version.