Adding a Block of Text

Question

How do you add a block of text to a plot?

Answer

The attributes of a block of text is described by two secondary objects, the "text-table" and "text-orientation" objects. The text-table object controls things like the font and color while the text-orientation object controls things like horizontal and vertical alignment and text height. You can mix-and-match these secondary objects to obtain the precise type of text you want.

You can create a single secondary "text-combined" object which has text-table and text-orientation objects as component objects. Let's start off with the following example line plot:

created by the following code:

import vcs
import Numeric as N
x = N.arange(17)*N.pi/8.
y = N.sin(x)
v = vcs.init()
my_gm = v.createxvsy('myGraphicsMethod','default')
my_gm.marker = 4
my_tpl = v.createtemplate('myTemplate', 'default')
my_tpl.mean.priority = 0
my_tpl.max.priority = 0
my_tpl.min.priority = 0
v.plot(x, y, my_gm, my_tpl, name=' ')

Note I've created my own graphics method and template for use in this plot. Also note that I set keyword name to whitespace, so that the default plot title is not printed.

Let's add a block of text on top for the title. To do so, we first create a text-combined object texttitle:

texttitle = v.createtext('title_ttab', 'default', 'title_tori', 'defcenter')

texttitle is made up of the text-table object with name 'title_ttab' and text-orientation object with name 'title_tori'. The text-table object is a copy of the default object, while the text-orientation object is a copy of the default-centered object (since we'll want the title to be centered). Note that v.createtext is an alias for v.createtextcombined.

Next we set the title height, the "x" (horizontal) and "y" (vertical) coordinate positions, and the content of the text block.

texttitle.height = 40
texttitle.x = [0.5]
texttitle.y = [0.9]
texttitle.string = ['Nice Sine Curve']

For some reason the VCS manual. doesn't seem to list string as an attribute of a text-combined object. That's unfortunate, since the content of the text block is set in string.

Note that the title's font height is in arbitrary units while the x and y coordinates are in normalized units. The x and y coordinates must be specified as a list or tuple.

We place the text on the plot using the plot command:

v.plot(texttitle)

and get:

You can specify more than one piece of text in the text-combined block by adding the additional text (and plot coordinates for that text) as additional elements of the list. This is one way of getting multiple lines of text. Thus:

texttitle.x = [0.5, 0.5]
texttitle.y = [0.95, 0.9]
texttitle.string = ['Nice Sine Curve', 'By Johnny']

gives us a two-line title with the first line "Nice Sine Curve" and the second line "By Johnny". Both lines are centered above the plot.


Notes: This discussion applies to CDAT 3.3.

Return to the Tips and Examples index page.

Updated: December 8, 2003 by Johnny Lin <email address>. License.