Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

r""" 

Interface to the Gnuplot interpreter 

""" 

 

#***************************************************************************** 

# Copyright (C) 2005 William Stein <wstein@gmail.com> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

from __future__ import print_function 

 

import os 

import time 

from sage.structure.sage_object import SageObject 

 

class Gnuplot(SageObject): 

""" 

Interface to the Gnuplot interpreter. 

""" 

def _quit_string(self): 

return 'quit' 

 

def gnuplot(self): 

try: 

return self._gnuplot 

except AttributeError: 

try: 

import Gnuplot as GP 

self._gnuplot = GP.Gnuplot() 

return self._gnuplot 

except ImportError: 

raise RuntimeError("Install the gnuplotpy Python module.") 

 

def __call__(self, line): 

return self.gnuplot()(line) 

 

def _eval_line(self, line, *args, **kwds): 

self(line) 

return '' 

 

def _repr_(self): 

""" 

TESTS:: 

 

sage: gnuplot # indirect doctests 

Interface to Gnuplot 

""" 

return "Interface to Gnuplot" 

 

def plot(self, cmd, file=None, verbose=True, reset=True): 

r""" 

Draw the plot described by cmd, and possibly also save to an eps or 

png file. 

 

INPUT: 

 

 

- ``cmd`` - string 

 

- ``file`` - string (default: None), if specified save 

plot to given file, which may be either an eps (default) or png 

file. 

 

- ``verbose`` - print some info 

 

- ``reset`` - True: reset gnuplot before making 

graph 

 

 

OUTPUT: displays graph 

 

.. note:: 

 

Note that ``^`` s are replaced by ``**`` s before being passed to gnuplot. 

""" 

if reset: 

self('reset') 

self('set terminal x11') 

cmd = cmd.replace('^','**') 

self(cmd) 

if file is not None: 

if file[-4:] == '.png': 

self('set terminal png medium') 

else: 

if file[-4:] != '.eps': 

file += '.eps' 

self('set terminal postscript eps enhanced') 

#self("set output '%s'"%file) 

tmp = 'gnuplot_tmp%s' % file[-4:] 

self("set output '%s'" % tmp) 

print("Saving plot to %s" % file) 

self(cmd) 

time.sleep(0.1) 

os.system('mv %s %s 2>/dev/null'%(tmp, file)) 

time.sleep(0.1) 

self('set terminal x11') 

 

def plot3d(self, f, xmin=-1, xmax=1, ymin=-1, ymax=1, zmin=-1, zmax=1, 

title=None, 

samples=25, isosamples=20, xlabel='x', ylabel='y', 

interact=True): 

if title is None: 

title = str(f) 

f = f.replace('^','**') 

cmd=""" 

set xlabel "%s" 

set ylabel "%s" 

set key top 

set border 4095 

set xrange [%s:%s] 

set yrange [%s:%s] 

set samples %s 

set isosamples %s 

 

set title "%s" 

set pm3d; set palette 

#show pm3d 

#show palette 

splot %s 

"""%(xlabel, ylabel, 

xmin, xmax, ymin, ymax, #zmin, zmax, 

samples, isosamples, 

title, f) 

if interact: 

self.interact(cmd) 

else: 

self(cmd) 

 

def plot3d_parametric(self, f='cos(u)*(3 + v*cos(u/2)), sin(u)*(3 + v*cos(u/2)), v*sin(u/2)', 

range1='[u=-pi:pi]', 

range2='[v=-0.2:0.2]', samples=50, title=None, 

interact=True): 

""" 

Draw a parametric 3d surface and rotate it interactively. 

 

INPUT: 

 

 

- ``f`` - (string) a function of two variables, e.g., 

'cos(u)\*(3 + v\*cos(u/2)), sin(u)\*(3 + v\*cos(u/2)), 

v\*sin(u/2)' 

 

- ``range1`` - (string) range of values for one 

variable, e.g., '[u=-pi:pi]' 

 

- ``range2`` - (string) range of values for another 

variable, e.g., '[v=-0.2:0.2]' 

 

- ``samples`` - (int) number of sample points to use 

 

- ``title`` - (string) title of the graph. 

 

 

EXAMPLES:: 

 

sage: gnuplot.plot3d_parametric('v^2*sin(u), v*cos(u), v*(1-v)') # optional - gnuplot (not tested, since something pops up). 

""" 

if title is None: 

title = str(f) 

cmd=""" 

set key top 

set border 4095 

set samples %s 

 

set title "%s" 

set pm3d; set palette; set parametric 

splot %s %s %s 

"""%(samples, title, range1, range2, f) 

cmd = cmd.replace('^','**') 

if interact: 

self.interact(cmd) 

else: 

self(cmd) 

 

 

def interact(self, cmd): 

from sage.misc.all import SAGE_TMP 

file = os.path.join(SAGE_TMP, 'gnuplot') 

open(file, 'w').write(cmd + '\n pause -1 "Press return to continue (no further rotation possible)"') 

os.system('sage-native-execute gnuplot -persist %s'%file) 

 

def console(self): 

gnuplot_console() 

 

# An instance 

gnuplot = Gnuplot() 

 

 

def gnuplot_console(): 

from sage.repl.rich_output.display_manager import get_display_manager 

if not get_display_manager().is_in_terminal(): 

raise RuntimeError('Can use the console only in the terminal. Try %%gnuplot magics instead.') 

os.system('sage-native-execute gnuplot')