~ellisonbg/ipython/bugfixes0411409

« back to all changes in this revision

Viewing changes to IPython/GnuplotInteractive.py

  • Committer: ville
  • Date: 2008-02-16 09:50:47 UTC
  • mto: (0.12.1 ipython_main)
  • mto: This revision was merged to the branch mainline in revision 990.
  • Revision ID: ville@ville-pc-20080216095047-500x6dluki1iz40o
initialization (no svn history)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
"""Interactive functions and magic functions for Gnuplot usage.
 
3
 
 
4
This requires the Gnuplot.py module for interfacing python with Gnuplot, which
 
5
can be downloaded from:
 
6
 
 
7
http://gnuplot-py.sourceforge.net/
 
8
 
 
9
See gphelp() below for details on the services offered by this module.
 
10
 
 
11
Inspired by a suggestion/request from Arnd Baecker.
 
12
 
 
13
$Id: GnuplotInteractive.py 389 2004-10-09 07:59:30Z fperez $"""
 
14
 
 
15
__all__ = ['Gnuplot','gp','gp_new','plot','plot2','splot','replot',
 
16
           'hardcopy','gpdata','gpfile','gpstring','gpfunc','gpgrid',
 
17
           'gphelp']
 
18
 
 
19
import IPython.GnuplotRuntime as GRun
 
20
from IPython.genutils import page,warn
 
21
 
 
22
# Set global names for interactive use
 
23
Gnuplot  = GRun.Gnuplot
 
24
gp_new   = GRun.gp_new
 
25
gp       = GRun.gp
 
26
plot     = gp.plot
 
27
plot2    = gp.plot2
 
28
splot    = gp.splot
 
29
replot   = gp.replot
 
30
hardcopy = gp.hardcopy
 
31
 
 
32
# Accessors for the main plot object constructors:
 
33
gpdata   = Gnuplot.Data
 
34
gpfile   = Gnuplot.File
 
35
gpstring = Gnuplot.String
 
36
gpfunc   = Gnuplot.Func
 
37
gpgrid   = Gnuplot.GridData
 
38
 
 
39
def gphelp():
 
40
    """Print information about the Gnuplot facilities in IPython."""
 
41
 
 
42
    page("""
 
43
IPython provides an interface to access the Gnuplot scientific plotting
 
44
system, in an environment similar to that of Mathematica or Matlab.
 
45
 
 
46
New top-level global objects
 
47
----------------------------
 
48
 
 
49
Please see their respective docstrings for further details.
 
50
 
 
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.
 
54
 
 
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:
 
57
 
 
58
In [1]: plot(x,sin(x),title='Sin(x)')  # assuming x is a Numeric array
 
59
 
 
60
and obtain a plot of sin(x) vs x with the title 'Sin(x)'.
 
61
 
 
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.
 
65
 
 
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).
 
69
 
 
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.
 
74
 
 
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
 
78
 
 
79
In [2]: gp('plot x,sin(x)')
 
80
 
 
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.
 
84
 
 
85
 
 
86
New magic functions
 
87
-------------------
 
88
 
 
89
%gpc: pass one command to Gnuplot and execute it or open a Gnuplot shell where
 
90
each line of input is executed.
 
91
 
 
92
%gp_set_default: reset the value of IPython's global Gnuplot instance.""")
 
93
    
 
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.
 
98
 
 
99
    Usage (omit the % if automagic is on). There are two ways to use it:
 
100
 
 
101
      1) %gpc 'command' -> passes 'command' directly to the gnuplot instance.
 
102
 
 
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.
 
106
 
 
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).
 
109
      """
 
110
 
 
111
    if parameter_s.strip():
 
112
        self.shell.gnuplot(parameter_s)
 
113
    else:
 
114
        self.shell.gnuplot.interact()
 
115
 
 
116
def magic_gp_set_default(self,parameter_s=''):
 
117
    """Set the default gnuplot instance accessed by the %gp magic function.
 
118
 
 
119
    %gp_set_default name
 
120
 
 
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
 
124
    instance object."""
 
125
 
 
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})
 
131
 
 
132
try:
 
133
    __IPYTHON__
 
134
except NameError:
 
135
    pass
 
136
else:
 
137
    # make the global Gnuplot instance known to IPython
 
138
    __IPYTHON__.gnuplot = GRun.gp
 
139
    __IPYTHON__.gnuplot.shell_first_time = 1
 
140
 
 
141
    print """*** Type `gphelp` for help on the Gnuplot integration features."""
 
142
 
 
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
 
147
 
 
148
#********************** End of file <GnuplotInteractive.py> *******************