Math and Numbers in Figure Text
Math and Numbers in Figure Text
The following matplotlib commands place text on a figure. (The tutorial on plotting
shows where they appear.)
xlabel label to the horizontal axis
ylabel label to the vertical axis
title add a title just above the axes
suptitle add a title farther above the axes
text add text at an arbitrary location in data coordinates
figtext add text at an arbitrary location in relative coordinates
annotate add an annotation, with optional arrow
It is often useful to include mathematical expressions and formatted numbers in the text.
r'$C = \frac{3}{4}$'
Preceding brackets with “\left” and “\right” automatically makes them the right
size for what’s inside. The following examples show how this can be useful.
r'$(\frac{5}{3})$' ( )
r'$\left(\frac{5}{3}\right)$'
Another frequently used TeX command is “\sqrt” for a square root symbol.
r'$\sqrt{xy} = 3$' xy = 3
You can look up other symbols at the website given below. This brief introduction should
be enough to get you started, but is not intended to be complete.
2
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.
figtext(0.5,0.5, 't = %3.1f +/- %3.1f s' % (t,sigmat))
The form of the formatting in a 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 TeX formatting can be combined (this is a
single command that wraps onto a second line).
figtext(0.5,0.5, r'$t = %3.1f \pm %3.1f \rm{ s}$' %
(t, sigmat) )