Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/interfaces/gnuplot.py
4036 views
1
r"""
2
Interface to the Gnuplot interpreter
3
"""
4
5
#*****************************************************************************
6
# Copyright (C) 2005 William Stein <[email protected]>
7
#
8
# Distributed under the terms of the GNU General Public License (GPL)
9
#
10
# This code is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
# General Public License for more details.
14
#
15
# The full text of the GPL is available at:
16
#
17
# http://www.gnu.org/licenses/
18
#*****************************************************************************
19
20
import os, time
21
from sage.structure.sage_object import SageObject
22
23
class Gnuplot(SageObject):
24
"""
25
Interface to the Gnuplot interpreter.
26
"""
27
def _quit_string(self):
28
return 'quit'
29
30
def gnuplot(self):
31
try:
32
return self._gnuplot
33
except AttributeError:
34
try:
35
import Gnuplot as GP
36
self._gnuplot = GP.Gnuplot()
37
return self._gnuplot
38
except ImportError:
39
raise RuntimeError, "Install the gnuplotpy Python module."
40
41
def __call__(self, line):
42
return self.gnuplot()(line)
43
44
def _eval_line(self, line, *args, **kwds):
45
self(line)
46
return ''
47
48
def __repr__(self):
49
return "Interface to Gnuplot"
50
51
def plot(self, cmd, file=None, verbose=True, reset=True):
52
r"""
53
Draw the plot described by cmd, and possibly also save to an eps or
54
png file.
55
56
INPUT:
57
58
59
- ``cmd`` - string
60
61
- ``file`` - string (default: None), if specified save
62
plot to given file, which may be either an eps (default) or png
63
file.
64
65
- ``verbose`` - print some info
66
67
- ``reset`` - True: reset gnuplot before making
68
graph
69
70
71
OUTPUT: displays graph
72
73
.. note::
74
75
Note that ``^`` s are replaced by ``**`` s before being passed to gnuplot.
76
"""
77
if reset:
78
self('reset')
79
self('set terminal x11')
80
cmd = cmd.replace('^','**')
81
self(cmd)
82
if file != None:
83
if file[-4:] == '.png':
84
self('set terminal png medium')
85
else:
86
if file[-4:] != '.eps':
87
file += '.eps'
88
self('set terminal postscript eps enhanced')
89
#self("set output '%s'"%file)
90
tmp = 'gnuplot_tmp%s'%file[-4:]
91
self("set output '%s'"%tmp)
92
print "Saving plot to %s"%file
93
self(cmd)
94
time.sleep(0.1)
95
os.system('mv %s %s 2>/dev/null'%(tmp, file))
96
time.sleep(0.1)
97
self('set terminal x11')
98
99
def plot3d(self, f, xmin=-1, xmax=1, ymin=-1, ymax=1, zmin=-1, zmax=1,
100
title=None,
101
samples=25, isosamples=20, xlabel='x', ylabel='y',
102
interact=True):
103
if title is None:
104
title = str(f)
105
f = f.replace('^','**')
106
cmd="""
107
set xlabel "%s"
108
set ylabel "%s"
109
set key top
110
set border 4095
111
set xrange [%s:%s]
112
set yrange [%s:%s]
113
set samples %s
114
set isosamples %s
115
116
set title "%s"
117
set pm3d; set palette
118
#show pm3d
119
#show palette
120
splot %s
121
"""%(xlabel, ylabel,
122
xmin, xmax, ymin, ymax, #zmin, zmax,
123
samples, isosamples,
124
title, f)
125
if interact:
126
self.interact(cmd)
127
else:
128
self(cmd)
129
130
def plot3d_parametric(self, f='cos(u)*(3 + v*cos(u/2)), sin(u)*(3 + v*cos(u/2)), v*sin(u/2)',
131
range1='[u=-pi:pi]',
132
range2='[v=-0.2:0.2]', samples=50, title=None,
133
interact=True):
134
"""
135
Draw a parametric 3d surface and rotate it interactively.
136
137
INPUT:
138
139
140
- ``f`` - (string) a function of two variables, e.g.,
141
'cos(u)\*(3 + v\*cos(u/2)), sin(u)\*(3 + v\*cos(u/2)),
142
v\*sin(u/2)'
143
144
- ``range1`` - (string) range of values for one
145
variable, e.g., '[u=-pi:pi]'
146
147
- ``range2`` - (string) range of values for another
148
variable, e.g., '[v=-0.2:0.2]'
149
150
- ``samples`` - (int) number of sample points to use
151
152
- ``title`` - (string) title of the graph.
153
154
155
EXAMPLES::
156
157
sage: gnuplot.plot3d_parametric('v^2*sin(u), v*cos(u), v*(1-v)') # optional -- requires gnuplot (not tested, since something pops up).
158
"""
159
if title is None:
160
title = str(f)
161
cmd="""
162
set key top
163
set border 4095
164
set samples %s
165
166
set title "%s"
167
set pm3d; set palette; set parametric
168
splot %s %s %s
169
"""%(samples, title, range1, range2, f)
170
cmd = cmd.replace('^','**')
171
if interact:
172
self.interact(cmd)
173
else:
174
self(cmd)
175
176
177
def interact(self, cmd):
178
from sage.misc.all import SAGE_TMP
179
file= '%s/gnuplot'%SAGE_TMP
180
open(file, 'w').write(cmd + '\n pause -1 "Press return to continue (no further rotation possible)"')
181
os.system('sage-native-execute gnuplot -persist %s'%file)
182
183
def console(self):
184
gnuplot_console()
185
186
# An instance
187
gnuplot = Gnuplot()
188
189
import os
190
def gnuplot_console():
191
os.system('sage-native-execute gnuplot')
192
193
194
195
196
197