Passing Python functions to Gnuplot -


plotting python function in gnuplot not straightforward although there solutions. example, 1 either cast values array or manually translate expression gnuplot’s syntax. here example uses module gnuplot.py interface:

#!/usr/bin/env python  import gnuplot import numpy np  ## define function ## func = lambda x, x0, y0, w: y0 * np.exp( -4*np.log(2) * ( (x-x0) / w )**2 ) # works regular function: # def func(x, x0, y0, w):     # return y0 * np.exp( -4*np.log(2) * ( (x-x0) / w )**2 ) popt = (10.1, 5, 2)  ## linspace ## x = np.linspace(0, 20, num=1000) # (x min, x max, number of points) y = func(x, *popt) func_linspace = gnuplot.data(x, y, with_='lines', title='linspace')  ## expression “translation” (lambda only) ## func_translation = gnuplot.func(     '{y0} * exp( -4*log(2) * ( (x-{x0}) / {w} )**2 )'.format(         x0=popt[0],         y0=popt[1],         w=popt[2],         ),     title='expression translation')  ## plot ## g = gnuplot.gnuplot() g.plot(func_linspace, func_translation) 

the first method works fine decent number of points fails when zooming-in or changing window out of array’s limits, while second 1 works @ zoom level. illustrate point, let’s zoom-in output of previous script:

function sampling vs expression translation

for reason, interesting find way plot python functions (lambda or regular functions) gnuplot functions. can think of 2 solution: automatically translating expression (works “simple” lambda functions”), or having gnuplot directly use python function.

first solution: expression translation (simple lambda functions only)

this method not tricky automate, impossible implement elaborate functions. still use method simple lambda functions. sketch behaviour of implementation:

>>> def lambda_to_gnuplot(func, popt): ...     # determine if translation possible ...     # extract function expression , replace parameters values ...     return func_expression # str >>> lambda_to_gnuplot( ...     lambda x, x0, y0, w: y0 * np.exp( -4*np.log(2) * ( (x-x0) / w )**2), ...     (10.1, 5, 2)) '5 * exp( -4*log(2) * ( (x-10.1) / 2 )**2 )' 

would there way implement lambda_to_gnuplot function in python?

second solution: directly passing python function gnuplot

the “perfect” solution having gnuplot use python function. in daring dreams, like:

>>> def func(x, x0, y0, w): ...     if x < x0: ...         return 0 ...     else: ...         return y0 * np.exp( -4*np.log(2) * ( (x-x0) / w )**2) >>> func_direct = gnuplot.pyfunction(lambda x: func(x, 10.1, 5, 2)) >>> g.plot(func_direct) 

this easiest solution use, implementation tough, if not impossible. any hints on how solution might implemented? answer may of course bypass gnuplot.py.

i not sure if i'm answering question, try executing python script system call within gnuplot passing argument(s).

for instance, imagine simple python script test.py:

import sys  x=float(sys.argv[1])  print x**2 

which return square of argument when called shell:

:~$ python test.py 2 4.0 :~$ python test.py 3 9.0 :~$ python test.py 4 16.0 

now, within gnuplot, turn function:

gnuplot> f(x) = real(system(sprintf("python test.py %g", x))) gnuplot> print f(1) 1.0 gnuplot> print f(2) 4.0 gnuplot> print f(3) 9.0 gnuplot> print f(4) 16.0 

i added real() string output system call converted float. allows use regular gnuplot function. don't need mention take lot longer execute plot x**2:

f(x) = real(system(sprintf("python test.py %g", x))) plot f(x) 

enter image description here


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -