1
# -*- coding: utf-8 -*-
2
"""Interactive functions and magic functions for Gnuplot usage.
4
This requires the Gnuplot.py module for interfacing python with Gnuplot, which
5
can be downloaded from:
7
http://gnuplot-py.sourceforge.net/
9
See gphelp() below for details on the services offered by this module.
11
Inspired by a suggestion/request from Arnd Baecker.
13
$Id: GnuplotInteractive.py 389 2004-10-09 07:59:30Z fperez $"""
15
__all__ = ['Gnuplot','gp','gp_new','plot','plot2','splot','replot',
16
'hardcopy','gpdata','gpfile','gpstring','gpfunc','gpgrid',
19
import IPython.GnuplotRuntime as GRun
20
from IPython.genutils import page,warn
22
# Set global names for interactive use
23
Gnuplot = GRun.Gnuplot
30
hardcopy = gp.hardcopy
32
# Accessors for the main plot object constructors:
35
gpstring = Gnuplot.String
37
gpgrid = Gnuplot.GridData
40
"""Print information about the Gnuplot facilities in IPython."""
43
IPython provides an interface to access the Gnuplot scientific plotting
44
system, in an environment similar to that of Mathematica or Matlab.
46
New top-level global objects
47
----------------------------
49
Please see their respective docstrings for further details.
51
- gp: a running Gnuplot instance. You can access its methods as
52
gp.<method>. gp(`a string`) will execute the given string as if it had been
53
typed in an interactive gnuplot window.
55
- plot, splot, replot and hardcopy: aliases to the methods of the same name in
56
the global running Gnuplot instance gp. These allow you to simply type:
58
In [1]: plot(x,sin(x),title='Sin(x)') # assuming x is a Numeric array
60
and obtain a plot of sin(x) vs x with the title 'Sin(x)'.
62
- gp_new: a function which returns a new Gnuplot instance. This can be used to
63
have multiple Gnuplot instances running in your session to compare different
64
plots, each in a separate window.
66
- Gnuplot: alias to the Gnuplot2 module, an improved drop-in replacement for
67
the original Gnuplot.py. Gnuplot2 needs Gnuplot but redefines several of its
68
functions with improved versions (Gnuplot2 comes with IPython).
70
- gpdata, gpfile, gpstring, gpfunc, gpgrid: aliases to Gnuplot.Data,
71
Gnuplot.File, Gnuplot.String, Gnuplot.Func and Gnuplot.GridData
72
respectively. These functions create objects which can then be passed to the
73
plotting commands. See the Gnuplot.py documentation for details.
75
Keep in mind that all commands passed to a Gnuplot instance are executed in
76
the Gnuplot namespace, where no Python variables exist. For example, for
77
plotting sin(x) vs x as above, typing
79
In [2]: gp('plot x,sin(x)')
81
would not work. Instead, you would get the plot of BOTH the functions 'x' and
82
'sin(x)', since Gnuplot doesn't know about the 'x' Python array. The plot()
83
method lives in python and does know about these variables.
89
%gpc: pass one command to Gnuplot and execute it or open a Gnuplot shell where
90
each line of input is executed.
92
%gp_set_default: reset the value of IPython's global Gnuplot instance.""")
94
# Code below is all for IPython use
95
# Define the magic functions for communicating with the above gnuplot instance.
96
def magic_gpc(self,parameter_s=''):
97
"""Execute a gnuplot command or open a gnuplot shell.
99
Usage (omit the % if automagic is on). There are two ways to use it:
101
1) %gpc 'command' -> passes 'command' directly to the gnuplot instance.
103
2) %gpc -> will open up a prompt (gnuplot>>>) which takes input like the
104
standard gnuplot interactive prompt. If you need to type a multi-line
105
command, use \\ at the end of each intermediate line.
107
Upon exiting of the gnuplot sub-shell, you return to your IPython
108
session (the gnuplot sub-shell can be invoked as many times as needed).
111
if parameter_s.strip():
112
self.shell.gnuplot(parameter_s)
114
self.shell.gnuplot.interact()
116
def magic_gp_set_default(self,parameter_s=''):
117
"""Set the default gnuplot instance accessed by the %gp magic function.
121
Call with the name of the new instance at the command line. If you want to
122
set this instance in your own code (using an embedded IPython, for
123
example), simply set the variable __IPYTHON__.gnuplot to your own gnuplot
126
gname = parameter_s.strip()
127
G = eval(gname,self.shell.user_ns)
128
self.shell.gnuplot = G
129
self.shell.user_ns.update({'plot':G.plot,'splot':G.splot,'plot2':G.plot2,
130
'replot':G.replot,'hardcopy':G.hardcopy})
137
# make the global Gnuplot instance known to IPython
138
__IPYTHON__.gnuplot = GRun.gp
139
__IPYTHON__.gnuplot.shell_first_time = 1
141
print """*** Type `gphelp` for help on the Gnuplot integration features."""
143
# Add the new magic functions to the class dict
144
from IPython.iplib import InteractiveShell
145
InteractiveShell.magic_gpc = magic_gpc
146
InteractiveShell.magic_gp_set_default = magic_gp_set_default
148
#********************** End of file <GnuplotInteractive.py> *******************