The following matplotlib commands place text on a figure as shown in the example below.
For all of these text commands, the size can be adjusted with the fontsize
argument. The color
argument can also be used with these command.
from IPython.display import Image
Image(filename="text-demo.png",width=500)
Mathematical expressions can be included in the text that any of the figure text commands produce by using LaTeX typesetting. In LaTeX, each symbol is represented by a string starting with a backslash (\
). For example, “\theta
” stands for the Greek letter theta ($\theta$). In ordinary strings, backslashes get interpreted in ways that would interfere with LaTeX typesetting. For example, “\t
” and “\n
” are ordinarily interpreted as a tab and a linefeed, respectively. The first example would cause problems when trying to typeset a theta symbol. To get around this difficulty, a string can be preceded by an “r
” so that it is treated as a raw string. Compare the output of the following print
commands.
print 'This is \t an \n ordinary string'
This is an ordinary string
print r'This is \t a \n raw string'
This is \t a \n raw string
In a string sent to one of the matplotlib commands, text in between a pair of dollar signs ($
) is interpreted as a mathematical expression typeset in LaTeX. The following example places the label “$\cos\theta$” on the vertical axis.
Regular text and math text can be combined within the same string. In the following example, the label “$\theta \rm{\ (radians)}$” is placed on the horizontal axis. The LaTeX command “\rm
” is used to make the font Roman for the plain text. Curly brackets must surround the text that you want to be affected. The "\
" (a backslash and a space) are used to insert a space after the theta.
%matplotlib inline
from pylab import *
figure()
xlim(0,10)
ylim(0,10)
ylabel(r'$\cos\theta$')
xlabel(r'$\theta \rm{\ (radians)}$')
show()
It is often useful to include numbers that are calculated by a program in figure text. Suppose that a program fits data to find a time (t
) is 5.43587 s and its uncertainty (sigmat
) is 0.21189 s. If you’re plotting the data, you might want to put the text “$t=5.4 \pm 0.2 \rm{\ s}$” on the figure. (Note that this is the correct number of decimal places to report because of the uncertainty.) You could use the figtext
command in the example below, but you’d have to change the numbers by hand if the data changed.
from pylab import *
figure()
xlim(0,10)
ylim(0,10)
figtext(0.5,0.5, 't = 5.4 +/- 0.2 s')
show()
It would be better to use the variables t
and sigmat
in the command as shown below. In this example, each copy of “%3.1f
” in the string is a format. The string is followed by a percent symbol (%
) and a list of variables to be formatted. The number of formats and variables must match.
from pylab import *
t = 5.43587
sigmat = 0.21189
figure()
xlim(0,10)
ylim(0,10)
figtext(0.5,0.5, 't = %3.1f +/- %3.1f s' % (t,sigmat))
show()
The form of the formatting string is “%(width).(precision)(specifier)
”, where width
specifies the maximum number of digits, precision
specifies the number of digits after the decimal point, and the possibilities for specifier
are shown below. For integer formatting, the precision argument is ignored if you give it. For scientific notation and floating point formatting, the width argument is optional.
Specifier | Meaning | Example Format | Output for −34.5678 |
---|---|---|---|
i | signed integer | %5i | -34 |
e | scientific notation | %5.4e | −3.4568e+001 |
f | floating point | %5.2f | −34.57 |
The formatting of the numerical output and LaTeX typesetting can be combined as shown in the example below. This gives the best results because the "$\pm$" can be typeset using "\pm
".
from pylab import *
t = 5.43587
sigmat = 0.21189
figure()
xlim(0,10)
ylim(0,10)
figtext(0.5,0.5, r'$t = %3.1f \pm %3.1f \rm{\ s}$' % (t, sigmat))
show()
More information is available at http://matplotlib.sourceforge.net/users/mathtext.html