Text in plots

class sage.plot.text.Text(string, point, options)[source]

Bases: GraphicPrimitive

Base class for Text graphics primitive.

../../_images/text-1.svg
get_minmax_data()[source]

Return 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.all import *
>>> T = text("Where am I?",(Integer(1),Integer(1)))
>>> t=T[Integer(0)]
>>> t.get_minmax_data()['ymin']
1.0
>>> t.get_minmax_data()['ymax']
1.0
plot3d(**kwds)[source]

Plot 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.all import *
>>> T = text("ABC", (Integer(1), Integer(1)))
>>> t = T[Integer(0)]
>>> s = t.plot3d()
>>> s.jmol_repr(s.testing_render_params())[Integer(0)][Integer(2)]
'label "ABC"'
>>> s._trans
(1.0, 1.0, 0)
sage.plot.text.text(string, xy, fontsize=10, rgbcolor=(0, 0, 1), horizontal_alignment='center', vertical_alignment='center', axis_coords=False, clip=False, **options)[source]

Return 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 – 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 – boolean (default: False); whether to clip or not

  • axis_coords – boolean (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 – 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
>>> from sage.all import *
>>> text("Sage graphics are really neat because they use matplotlib!", (Integer(2),Integer(12)))
Graphics object consisting of 1 graphics primitive
../../_images/text-2.svg

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
>>> from sage.all import *
>>> text("I had a dream!", (Integer(2),Integer(12)), alpha=RealNumber('0.3'),
...      fontsize='large', fontweight='bold', color='red')
Graphics object consisting of 1 graphics primitive
../../_images/text-3.svg

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
>>> from sage.all import *
>>> text("I got a horse and he lives in a tree", (Integer(0),Integer(0)),
...      axis_coords=True, horizontal_alignment='left')
Graphics object consisting of 1 graphics primitive
../../_images/text-4.svg

Various rotations:

sage: text("noitator", (0,0), rotation=45.0,
....:      horizontal_alignment='left', vertical_alignment='bottom')
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> text("noitator", (Integer(0),Integer(0)), rotation=RealNumber('45.0'),
...      horizontal_alignment='left', vertical_alignment='bottom')
Graphics object consisting of 1 graphics primitive
../../_images/text-5.svg
sage: text("Sage is really neat!!", (0,0), rotation='vertical')
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> text("Sage is really neat!!", (Integer(0),Integer(0)), rotation='vertical')
Graphics object consisting of 1 graphics primitive
../../_images/text-6.svg

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
>>> from sage.all import *
>>> t1 = text("Hello", (Integer(1),Integer(1)), vertical_alignment='top')
>>> t2 = text("World", (Integer(1),RealNumber('0.5')), horizontal_alignment='left')
>>> t1 + t2   # render the sum
Graphics object consisting of 2 graphics primitives
../../_images/text-7.svg

You can save text as part of PDF output:

sage: import tempfile
sage: with tempfile.NamedTemporaryFile(suffix='.pdf') as f:
....:     text("sage", (0,0), rgbcolor=(0,0,0)).save(f.name)
>>> from sage.all import *
>>> import tempfile
>>> with tempfile.NamedTemporaryFile(suffix='.pdf') as f:
...     text("sage", (Integer(0),Integer(0)), rgbcolor=(Integer(0),Integer(0),Integer(0))).save(f.name)

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
>>> from sage.all import *
>>> bbox = {'boxstyle': "rarrow,pad=0.3", 'fc': "cyan", 'ec': "b", 'lw': Integer(2)}
>>> text("I feel good", (Integer(1),Integer(2)), bounding_box=bbox)
Graphics object consisting of 1 graphics primitive
../../_images/text-8.svg
sage: text("So good", (0,0), bounding_box={'boxstyle': 'round', 'fc': 'w'})
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> text("So good", (Integer(0),Integer(0)), bounding_box={'boxstyle': 'round', 'fc': 'w'})
Graphics object consisting of 1 graphics primitive
../../_images/text-9.svg

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
>>> from sage.all import *
>>> text("So good", (-Integer(2),Integer(2)), background_color='red')
Graphics object consisting of 1 graphics primitive
../../_images/text-10.svg

Use dollar signs for LaTeX and raw strings to avoid having to escape backslash characters:

sage: A = arc((0, 0), 1, sector=(0.0, RDF.pi()))
sage: a = sqrt(1./2.)
sage: PQ = point2d([(-a, a), (a, a)])
sage: botleft = dict(horizontal_alignment='left', vertical_alignment='bottom')
sage: botright = dict(horizontal_alignment='right', vertical_alignment='bottom')
sage: tp = text(r'$z_P = e^{3i\pi/4}$',
....:           (-a, a), **botright)
sage: tq = text(r'$Q = (\frac{\sqrt{2}}{2}, \frac{\sqrt{2}}{2})$',
....:           (a, a), **botleft)
sage: A + PQ + tp + tq
Graphics object consisting of 4 graphics primitives
>>> from sage.all import *
>>> A = arc((Integer(0), Integer(0)), Integer(1), sector=(RealNumber('0.0'), RDF.pi()))
>>> a = sqrt(RealNumber('1.')/RealNumber('2.'))
>>> PQ = point2d([(-a, a), (a, a)])
>>> botleft = dict(horizontal_alignment='left', vertical_alignment='bottom')
>>> botright = dict(horizontal_alignment='right', vertical_alignment='bottom')
>>> tp = text(r'$z_P = e^{3i\pi/4}$',
...           (-a, a), **botright)
>>> tq = text(r'$Q = (\frac{\sqrt{2}}{2}, \frac{\sqrt{2}}{2})$',
...           (a, a), **botleft)
>>> A + PQ + tp + tq
Graphics object consisting of 4 graphics primitives
../../_images/text-11.svg

Text coordinates must be 2D, an error is raised if 3D coordinates are passed:

sage: t = text("hi", (1, 2, 3))
Traceback (most recent call last):
...
ValueError: use text3d instead for text in 3d
>>> from sage.all import *
>>> t = text("hi", (Integer(1), Integer(2), Integer(3)))
Traceback (most recent call last):
...
ValueError: use text3d instead for text in 3d

Use the text3d function for 3D text:

sage: t = text3d("hi", (1, 2, 3))
>>> from sage.all import *
>>> t = text3d("hi", (Integer(1), Integer(2), Integer(3)))

Or produce 2D text with coordinates \((x, y)\) and plot it in 3D (at \(z = 0\)):

sage: t = text("hi", (1, 2))
sage: t.plot3d()  # text at position (1, 2, 0)
Graphics3d Object
>>> from sage.all import *
>>> t = text("hi", (Integer(1), Integer(2)))
>>> t.plot3d()  # text at position (1, 2, 0)
Graphics3d Object

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

sage: text("MATH IS AWESOME", (0, 0), fontsize=40, axes=False)
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> text("MATH IS AWESOME", (Integer(0), Integer(0)), fontsize=Integer(40), axes=False)
Graphics object consisting of 1 graphics primitive

is equivalent to

sage: text("MATH IS AWESOME", (0, 0), fontsize=40).show(axes=False)
>>> from sage.all import *
>>> text("MATH IS AWESOME", (Integer(0), Integer(0)), fontsize=Integer(40)).show(axes=False)