Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Text, Math, and Numbers in Figures
1. Adding Text to Figures
The following matplotlib commands place text on a figure as shown in the example below.
-
xlabel - label to the horizontal axis
ylabel - label the vertical axis
title - add a title just above the axes
suptitle - add a smaller 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 an arrow
The main difference between text and figtext is how the location is specified. For text, the location of the starting point is given in terms of the data coordinates. For figtext, the location of the is given in terms of the fractions of the horizontal and vertical size of the graph measured from the lower, left corner.
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.
2. Math in Figure Text
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 (). 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.
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 “” on the vertical axis.
Regular text and math text can be combined within the same string. In the following example, the label “” 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.
3. Formatting Numbers
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 is 0.21189 s. If you’re plotting the data, you might want to put the text “” 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.
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.
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 "" can be typeset using "\pm".
Additional Documentation
More information is available at https://matplotlib.org/stable/tutorials/text/text_intro.html Also, see the tutorial on typesetting mathematics with LaTeX.