[IPython-user] gnuplot variables

Fernando Perez fperez at colorado.edu
Thu Jul 31 12:26:54 CDT 2003

Arnd Baecker wrote:

> Background:
> There are a couple of interesting developments going on
> in the CVS version of gnuplot. In particular,
> with recent additions it is possible to get the coordinates
> of a mouse click as variables in gnuplot:
>   pause mouse "Click on one corner of desired zoom region"
>   print "Mouse button ", MOUSE_BUTTON, " at ", MOUSE_X, " ", MOUSE_Y ;\
> With this addition and the Python gnuplot support this
> opens quite interesting possibilities.
> However, there is one thing we would need from the python side,
> namely, the ability to access the gnuplot variables from within python.

Right after I sent my reply, I saw a message in my inbox from M. Haggerty (the 
original author of Gnuplot.py), from the Gnuplot.py mailing list, which seems 

I reproduce it below in case you find it useful.



---------- Original message follows:

Ido Yehieli wrote:

 >>   is it possible to use gnuplot's 'fit' command
 >>throw Gnuplot.py?

As the others have pointed out, you can call any gnuplot command from
Gnuplot.py.  The other issue with "fit", however, is how do you get the
results of the fit back into Python?  This is a problem because
Gnuplot.py doesn't have access to gnuplot's standard output.

Luckily this is easy--you write the parameters to a "parameters file"
(which fortuitously is valid python code) then read them back into
python.  This is untested but should approximately work:

  >>> g = Gnuplot.Gnuplot()
  >>> open('fit.par', 'w').write('m=0\nb=0\n')
  >>> g('fit m*x+b "data.in" via "fit.par"')
  >>> g('update "fit.par" "fit2.par"') # saves fitted parameters to fit2.par
  >>> parameters = {}
  >>> execfile('fit2.par', globals(), parameters)
  >>> print parameters
{'b': 1.5015, 'm': 1e-30}
  >>> print parameters['m'], parameters['b']
1e-30 1.5015

In principle you could also parse "fit.log" to get the gory details
about the fit (standard errors, correlation matrix, etc).

Hope this helps,

-- Michael Haggerty mhagger at alum.mit.edu

More information about the IPython-user mailing list