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

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

""" 

Text in plots 

""" 

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

# Copyright (C) 2006 Alex Clemesha <clemesha@gmail.com>, 

# William Stein <wstein@gmail.com>, 

# 2008 Mike Hansen <mhansen@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 sage.plot.primitive import GraphicPrimitive 

from sage.misc.decorators import options, rename_keyword 

from sage.plot.colors import to_mpl_color 

 

 

class Text(GraphicPrimitive): 

""" 

Base class for Text graphics primitive. 

 

TESTS: 

 

We test creating some text:: 

 

sage: text("I like Fibonacci",(3,5)) 

Graphics object consisting of 1 graphics primitive 

 

.. PLOT:: 

 

sphinx_plot(text("I like Fibonacci",(3,5))) 

 

""" 

def __init__(self, string, point, options): 

""" 

Initializes base class Text. 

 

EXAMPLES:: 

 

sage: T = text("I like Fibonacci", (3,5)) 

sage: t = T[0] 

sage: t.string 

'I like Fibonacci' 

sage: t.x 

3.0 

sage: t.options()['fontsize'] 

10 

""" 

self.string = string 

self.x = float(point[0]) 

self.y = float(point[1]) 

GraphicPrimitive.__init__(self, options) 

 

def get_minmax_data(self): 

""" 

Returns a dictionary with the bounding box data. Notice 

that, for text, the box is just the location itself. 

 

EXAMPLES:: 

 

sage: T = text("Where am I?",(1,1)) 

sage: t=T[0] 

sage: t.get_minmax_data()['ymin'] 

1.0 

sage: t.get_minmax_data()['ymax'] 

1.0 

""" 

from sage.plot.plot import minmax_data 

return minmax_data([self.x], [self.y], dict=True) 

 

def _repr_(self): 

""" 

String representation of Text primitive. 

 

EXAMPLES:: 

 

sage: T = text("I like cool constants", (pi,e)) 

sage: t=T[0];t 

Text 'I like cool constants' at the point (3.14159265359,2.71828182846) 

""" 

return "Text '%s' at the point (%s,%s)"%(self.string, self.x, self.y) 

 

def _allowed_options(self): 

""" 

Return the allowed options for the Text class. 

 

EXAMPLES:: 

 

sage: T = text("ABC",(1,1),zorder=3) 

sage: T[0]._allowed_options()['fontsize'] 

"How big the text is. Either the size in points or a relative size, e.g. 'smaller', 'x-large', etc" 

sage: T[0]._allowed_options()['zorder'] 

'The layer level in which to draw' 

sage: T[0]._allowed_options()['rotation'] 

'How to rotate the text: angle in degrees, vertical, horizontal' 

 

""" 

return {'fontsize': 'How big the text is. Either the size in points or a relative size, e.g. \'smaller\', \'x-large\', etc', 

'fontstyle': 'A string either \'normal\', \'italic\' or \'oblique\'', 

'fontweight': 'A numeric value in the range 0-1000 or a string' 

'\'ultralight\', \'light\', \'normal\', \'regular\', \'book\',' 

'\'medium\', \'roman\', \'semibold\', \'demibold\', \'demi\',' 

'\'bold,\', \'heavy\', \'extra bold\', \'black\'', 

'rgbcolor': 'The color as an RGB tuple', 

'background_color': 'The background color', 

'bounding_box': 'A dictionary specifying a bounding box', 

'hue': 'The color given as a hue', 

'alpha': 'A float (0.0 transparent through 1.0 opaque)', 

'axis_coords': 'If True use axis coordinates: (0,0) lower left and (1,1) upper right', 

'rotation': 'How to rotate the text: angle in degrees, vertical, horizontal', 

'vertical_alignment': 'How to align vertically: top, center, bottom', 

'horizontal_alignment': 'How to align horizontally: left, center, right', 

'zorder': 'The layer level in which to draw', 

'clip': 'Whether to clip or not'} 

 

def _plot3d_options(self, options=None): 

""" 

Translate 2D plot options into 3D plot options. 

 

EXAMPLES:: 

 

sage: T = text("ABC",(1,1)) 

sage: t = T[0] 

sage: t.options()['rgbcolor'] 

(0.0, 0.0, 1.0) 

sage: s=t.plot3d() 

sage: s.jmol_repr(s.testing_render_params())[0][1] 

'color atom [0,0,255]' 

 

""" 

if options is None: 

options = dict(self.options()) 

options_3d = {} 

# TODO: figure out how to implement rather than ignore 

for s in ['axis_coords', 'clip', 'fontsize', 'horizontal_alignment', 

'rotation', 'vertical_alignment']: 

if s in options: 

del options[s] 

options_3d.update(GraphicPrimitive._plot3d_options(self, options)) 

return options_3d 

 

def plot3d(self, **kwds): 

""" 

Plots 2D text in 3D. 

 

EXAMPLES:: 

 

sage: T = text("ABC",(1,1)) 

sage: t = T[0] 

sage: s=t.plot3d() 

sage: s.jmol_repr(s.testing_render_params())[0][2] 

'label "ABC"' 

sage: s._trans 

(1.0, 1.0, 0) 

 

""" 

from sage.plot.plot3d.shapes2 import text3d 

options = self._plot3d_options() 

options.update(kwds) 

return text3d(self.string, (self.x, self.y, 0), **options) 

 

def _render_on_subplot(self, subplot): 

""" 

TESTS:: 

 

sage: t1 = text("Hello",(1,1), vertical_alignment="top", fontsize=30, rgbcolor='black') 

sage: t2 = text("World", (1,1), horizontal_alignment="left", fontsize=20, zorder=-1) 

sage: t1 + t2 # render the sum 

Graphics object consisting of 2 graphics primitives 

 

""" 

options = self.options() 

opts = {} 

opts['color'] = options['rgbcolor'] 

opts['verticalalignment'] = options['vertical_alignment'] 

opts['horizontalalignment'] = options['horizontal_alignment'] 

if 'background_color' in options: 

opts['backgroundcolor'] = options['background_color'] 

if 'fontweight' in options: 

opts['fontweight'] = options['fontweight'] 

if 'alpha' in options: 

opts['alpha'] = options['alpha'] 

if 'fontstyle' in options: 

opts['fontstyle'] = options['fontstyle'] 

if 'bounding_box' in options: 

opts['bbox'] = options['bounding_box'] 

if 'zorder' in options: 

opts['zorder'] = options['zorder'] 

if options['axis_coords']: 

opts['transform'] = subplot.transAxes 

if 'fontsize' in options: 

val = options['fontsize'] 

if isinstance(val, str): 

opts['fontsize'] = val 

else: 

opts['fontsize'] = int(val) 

if 'rotation' in options: 

val = options['rotation'] 

if isinstance(val, str): 

opts['rotation'] = options['rotation'] 

else: 

opts['rotation'] = float(options['rotation']) 

 

p = subplot.text(self.x, self.y, self.string, clip_on=options['clip'], **opts) 

if not options['clip']: 

self._bbox_extra_artists = [p] 

 

 

@rename_keyword(color='rgbcolor') 

@options(fontsize=10, rgbcolor=(0,0,1), horizontal_alignment='center', 

vertical_alignment='center', axis_coords=False, clip=False) 

def text(string, xy, **options): 

r""" 

Returns a 2D text graphics object at the point `(x,y)`. 

 

Type ``text.options`` for a dictionary of options for 2D text. 

 

2D OPTIONS: 

 

- ``fontsize`` - How big the text is. Either an integer that 

specifies the size in points or a string which specifies a size (one of 

'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large') 

 

- ``fontstyle`` - A string either 'normal', 'italic' or 'oblique' 

 

- ``fontweight`` - A numeric value in the range 0-1000 or a string (one of 

'ultralight', 'light', 'normal', 'regular', 'book',' 'medium', 'roman', 

'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black') 

 

- ``rgbcolor`` - The color as an RGB tuple 

 

- ``hue`` - The color given as a hue 

 

- ``alpha`` - A float (0.0 transparent through 1.0 opaque) 

 

- ``background_color`` - The background color 

 

- ``rotation`` - How to rotate the text: angle in degrees, vertical, horizontal 

 

- ``vertical_alignment`` - How to align vertically: top, center, bottom 

 

- ``horizontal_alignment`` - How to align horizontally: left, center, right 

 

- ``zorder`` - The layer level in which to draw 

 

- ``clip`` - (default: False) Whether to clip or not 

 

- ``axis_coords`` - (default: False) If True, use axis coordinates, so that 

(0,0) is the lower left and (1,1) upper right, regardless of the x and y 

range of plotted values. 

 

- ``bounding_box`` - A dictionary specifying a bounding box. Currently the text location. 

 

EXAMPLES:: 

 

sage: text("Sage graphics are really neat because they use matplotlib!", (2,12)) 

Graphics object consisting of 1 graphics primitive 

 

.. PLOT:: 

 

t = "Sage graphics are really neat because they use matplotlib!" 

sphinx_plot(text(t,(2,12))) 

 

Larger font, bold, colored red and transparent text:: 

 

sage: text("I had a dream!", (2,12), alpha=0.3, fontsize='large', fontweight='bold', color='red') 

Graphics object consisting of 1 graphics primitive 

 

.. PLOT:: 

 

sphinx_plot(text("I had a dream!", (2,12), alpha=0.3, fontsize='large', fontweight='bold', color='red')) 

 

By setting ``horizontal_alignment`` to 'left' the text is guaranteed to be 

in the lower left no matter what:: 

 

sage: text("I got a horse and he lives in a tree", (0,0), axis_coords=True, horizontal_alignment='left') 

Graphics object consisting of 1 graphics primitive 

 

.. PLOT:: 

 

t = "I got a horse and he lives in a tree" 

sphinx_plot(text(t, (0,0), axis_coords=True, horizontal_alignment='left')) 

 

Various rotations:: 

 

sage: text("noitator", (0,0), rotation=45.0, horizontal_alignment='left', vertical_alignment='bottom') 

Graphics object consisting of 1 graphics primitive 

 

.. PLOT:: 

 

sphinx_plot(text("noitator", (0,0), rotation=45.0, horizontal_alignment='left', vertical_alignment='bottom')) 

 

:: 

 

sage: text("Sage is really neat!!",(0,0), rotation="vertical") 

Graphics object consisting of 1 graphics primitive 

 

.. PLOT:: 

 

sphinx_plot(text("Sage is really neat!!",(0,0), rotation="vertical")) 

 

You can also align text differently:: 

 

sage: t1 = text("Hello",(1,1), vertical_alignment="top") 

sage: t2 = text("World", (1,0.5), horizontal_alignment="left") 

sage: t1 + t2 # render the sum 

Graphics object consisting of 2 graphics primitives 

 

.. PLOT:: 

 

t1 = text("Hello",(1,1), vertical_alignment="top") 

t2 = text("World", (1,0.5), horizontal_alignment="left") 

sphinx_plot(t1 + t2) 

 

You can save text as part of PDF output:: 

 

sage: text("sage", (0,0), rgbcolor=(0,0,0)).save(os.path.join(SAGE_TMP, 'a.pdf')) 

 

Some examples of bounding box:: 

 

sage: bbox = {'boxstyle':"rarrow,pad=0.3", 'fc':"cyan", 'ec':"b", 'lw':2} 

sage: text("I feel good", (1,2), bounding_box=bbox) 

Graphics object consisting of 1 graphics primitive 

 

.. PLOT:: 

 

bbox = {'boxstyle':"rarrow,pad=0.3", 'fc':"cyan", 'ec':"b", 'lw':2} 

sphinx_plot(text("I feel good", (1,2), bounding_box=bbox)) 

 

:: 

 

sage: text("So good", (0,0), bounding_box={'boxstyle':'round', 'fc':'w'}) 

Graphics object consisting of 1 graphics primitive 

 

.. PLOT:: 

 

bbox = {'boxstyle':'round', 'fc':'w'} 

sphinx_plot(text("So good", (0,0), bounding_box=bbox)) 

 

The possible options of the bounding box are 'boxstyle' (one of 'larrow', 

'rarrow', 'round', 'round4', 'roundtooth', 'sawtooth', 'square'), 'fc' or 

'facecolor', 'ec' or 'edgecolor', 'ha' or 'horizontalalignment', 'va' or 

'verticalalignment', 'lw' or 'linewidth'. 

 

A text with a background color:: 

 

sage: text("So good", (-2,2), background_color='red') 

Graphics object consisting of 1 graphics primitive 

 

.. PLOT:: 

 

sphinx_plot(text("So good", (-2,2), background_color='red')) 

 

Text must be 2D (use the text3d command for 3D text):: 

 

sage: t = text("hi",(1,2,3)) 

Traceback (most recent call last): 

... 

ValueError: use text3d instead for text in 3d 

sage: t = text3d("hi",(1,2,3)) 

 

Extra options will get passed on to show(), as long as they are valid:: 

 

sage: text("MATH IS AWESOME", (0, 0), fontsize=40, axes=False) 

Graphics object consisting of 1 graphics primitive 

sage: text("MATH IS AWESOME", (0, 0), fontsize=40).show(axes=False) # These are equivalent 

""" 

try: 

x, y = xy 

except ValueError: 

if isinstance(xy, (list, tuple)) and len(xy) == 3: 

raise ValueError("use text3d instead for text in 3d") 

raise 

from sage.plot.all import Graphics 

options['rgbcolor'] = to_mpl_color(options['rgbcolor']) 

point = (float(x), float(y)) 

g = Graphics() 

g._set_extra_kwds(Graphics._extract_kwds_for_show(options, ignore='fontsize')) 

g.add_primitive(Text(string, point, options)) 

return g