|
| 1 | +Writing mathematical expressions |
| 2 | +================================ |
| 3 | + |
| 4 | +You can use TeX markup in any matplotlib text string; see the |
| 5 | +:mod:`matplotlib.mathtext` module documentation for details. Note |
| 6 | +that you do not need to have TeX installed, since matplotlib ships its |
| 7 | +own TeX expression parser, layout engine and fonts. The layout engine |
| 8 | +is a fairly direct adaptation of the layout algorithms in Donald |
| 9 | +Knuth's TeX, so the quality is quite good (matplotlib also provides a |
| 10 | +``usetex`` option for those who do want to call out to TeX to generate |
| 11 | +their text). |
| 12 | + |
| 13 | +Any text element can use math text. You need to use raw strings |
| 14 | +(preceed the quotes with an ``'r'``), and surround the string text |
| 15 | +with dollar signs, as in TeX. Regular text and mathtext can be |
| 16 | +interleaved within the same string. Mathtext can use the Computer |
| 17 | +Modern fonts (from (La)TeX), `STIX <http://www.aip.org/stixfonts/>`_ |
| 18 | +fonts (with are designed to blend well with Times) or a Unicode font |
| 19 | +that you provide. The mathtext font can be selected with the |
| 20 | +customization variable ``mathtext.fontset``. |
| 21 | + |
| 22 | +Here is a simple example:: |
| 23 | + |
| 24 | + # plain text |
| 25 | + plt.title('alpha > beta') |
| 26 | + |
| 27 | +produces "alpha > beta". |
| 28 | + |
| 29 | +Whereas this:: |
| 30 | + |
| 31 | + # math text |
| 32 | + plt.title(r'$\alpha > \beta$') |
| 33 | + |
| 34 | +produces ":math:`\alpha > \beta`". |
| 35 | + |
| 36 | +.. TODO: Include a complete list here |
| 37 | +
|
| 38 | +Subscripts and superscripts |
| 39 | +--------------------------- |
| 40 | + |
| 41 | +To make subscripts and superscripts, use the ``'_'`` and ``'^'`` symbols:: |
| 42 | + |
| 43 | + r'$\alpha_i > \beta_i$' |
| 44 | + |
| 45 | +.. math:: |
| 46 | +
|
| 47 | + \alpha_i > \beta_i |
| 48 | +
|
| 49 | +Some symbols automatically put their sub/superscripts under and over |
| 50 | +the operator. For example, to write the sum of :math:`x_i` from :math:`0` to |
| 51 | +:math:`\infty`, you could do:: |
| 52 | + |
| 53 | + r'$\sum_{i=0}^\infty x_i$' |
| 54 | + |
| 55 | +.. math:: |
| 56 | +
|
| 57 | + \sum_{i=0}^\infty x_i |
| 58 | +
|
| 59 | +Fractions |
| 60 | +--------- |
| 61 | + |
| 62 | +Fractions can be created with the ``\frac{}{}`` command:: |
| 63 | + |
| 64 | + r'$\frac{3}{4}$' |
| 65 | + |
| 66 | +produces |
| 67 | + |
| 68 | +.. math:: |
| 69 | +
|
| 70 | + \frac{3}{4} |
| 71 | +
|
| 72 | +Fractions can be arbitrarily nested:: |
| 73 | + |
| 74 | + r'$\frac{5 - \frac{1}{x}}{4}$' |
| 75 | + |
| 76 | +produces |
| 77 | + |
| 78 | +.. math:: |
| 79 | +
|
| 80 | + \frac{5 - \frac{1}{x}}{4} |
| 81 | +
|
| 82 | +Note that special care needs to be taken to place parentheses and brackets around |
| 83 | +fractions. Doing things the obvious way produces brackets that are |
| 84 | +too small:: |
| 85 | + |
| 86 | + r'$(\frac{5 - \frac{1}{x}}{4})$' |
| 87 | + |
| 88 | +.. math :: |
| 89 | +
|
| 90 | + (\frac{5 - \frac{1}{x}}{4}) |
| 91 | +
|
| 92 | +The solution is to precede the bracket with ``\left`` and ``\right`` |
| 93 | +to inform the parser that those brackets encompass the entire object:: |
| 94 | + |
| 95 | + r'$\left(\frac{5 - \frac{1}{x}}{4}\right)$' |
| 96 | + |
| 97 | +.. math :: |
| 98 | +
|
| 99 | + \left(\frac{5 - \frac{1}{x}}{4}\right) |
| 100 | +
|
| 101 | +Radicals |
| 102 | +-------- |
| 103 | + |
| 104 | +Radicals can be produced with the ``\sqrt[]{}`` command. For example: |
| 105 | + |
| 106 | + r'$\sqrt{2}$' |
| 107 | + |
| 108 | +.. math :: |
| 109 | +
|
| 110 | + \sqrt{2} |
| 111 | +
|
| 112 | +Any base can (optionally) be provided inside square brackets. Note |
| 113 | +that the base must be a simple expression, and can not contain layout |
| 114 | +commands such as fractions or sub/superscripts. |
| 115 | + |
| 116 | + r'$\sqrt[3]{x}$' |
| 117 | + |
| 118 | +.. math :: |
| 119 | +
|
| 120 | + \sqrt[3]{x} |
| 121 | +
|
| 122 | +Fonts |
| 123 | +----- |
| 124 | + |
| 125 | +The default font is *italics* for mathematical symbols. To change |
| 126 | +fonts, eg, to write "sin" in a Roman font, enclose the text in a font |
| 127 | +command:: |
| 128 | + |
| 129 | + r'$s(t) = \mathcal{A}\mathrm{sin}(2 \omega t)$' |
| 130 | + |
| 131 | +.. math:: |
| 132 | +
|
| 133 | + s(t) = \mathcal{A}\mathrm{sin}(2 \omega t) |
| 134 | +
|
| 135 | +More conveniently, many commonly used function names that are typeset in a |
| 136 | +Roman font have shortcuts. So the expression above could be written |
| 137 | +as follows:: |
| 138 | + |
| 139 | + r'$s(t) = \mathcal{A}\sin(2 \omega t)$' |
| 140 | + |
| 141 | +.. math:: |
| 142 | +
|
| 143 | + s(t) = \mathcal{A}\sin(2 \omega t) |
| 144 | +
|
| 145 | +Here "s" and "t" are variable in italics font (default), "sin" is in |
| 146 | +Roman font, and the amplitude "A" is in calligraphy font. |
| 147 | + |
| 148 | +The choices available with all fonts are: |
| 149 | + |
| 150 | + =============== ================================= |
| 151 | + Command Result |
| 152 | + =============== ================================= |
| 153 | + ``\mathrm`` :math:`\mathrm{Roman}` |
| 154 | + ``\mathit`` :math:`\mathit{Italic}` |
| 155 | + ``\mathtt`` :math:`\mathtt{Typewriter}` |
| 156 | + ``\mathcal`` :math:`\mathcal{CALLIGRAPHY}` |
| 157 | + =============== ================================= |
| 158 | + |
| 159 | +When using the STIX fonts, you also have the choice of: |
| 160 | + |
| 161 | + ================ ================================= |
| 162 | + Command Result |
| 163 | + ================ ================================= |
| 164 | + ``\mathbb`` :math:`\mathbb{Blackboard}` |
| 165 | + ``\mathcircled`` :math:`\mathcircled{Circled}` |
| 166 | + ``\mathfrak`` :math:`\mathfrak{Fraktur}` |
| 167 | + ``\mathsf`` :math:`\mathsf{sans-serif}` |
| 168 | + ================ ================================= |
| 169 | + |
| 170 | +Accents |
| 171 | +------- |
| 172 | + |
| 173 | +An accent command may precede any symbol to add an accent above it. |
| 174 | +There are long and short forms for some of them. |
| 175 | + |
| 176 | + ============================== ================================= |
| 177 | + Command Result |
| 178 | + ============================== ================================= |
| 179 | + ``\acute a`` or ``\'a`` :math:`\acute a` |
| 180 | + ``\bar a`` :math:`\bar a` |
| 181 | + ``\breve a`` :math:`\breve a` |
| 182 | + ``\ddot a`` or ``\"a`` :math:`\ddot a` |
| 183 | + ``\dot a`` or ``\.a`` :math:`\dot a` |
| 184 | + ``\grave a`` or ``\\`a`` :math:`\grave a` |
| 185 | + ``\hat a`` or ``\^a`` :math:`\hat a` |
| 186 | + ``\tilde a`` or ``\~a`` :math:`\tilde a` |
| 187 | + ``\vec a`` :math:`\vec a` |
| 188 | + ============================== ================================= |
| 189 | + |
| 190 | +In addition, there are two special accents that automatically adjust |
| 191 | +to the width of the symbols below: |
| 192 | + |
| 193 | + ============================== ================================= |
| 194 | + Command Result |
| 195 | + ============================== ================================= |
| 196 | + ``\widehat{xyz}`` :math:`\widehat{xyz}` |
| 197 | + ``\widetilde{xyz}`` :math:`\widetilde{xyz}` |
| 198 | + ============================== ================================= |
| 199 | + |
| 200 | + |
| 201 | +Symbols |
| 202 | +------- |
| 203 | + |
| 204 | +You can also use a large number of the TeX symbols, as in ``\infty``, |
| 205 | +``\leftarrow``, ``\sum``, ``\int``; see :class:`matplotlib.mathtext` for a |
| 206 | +complete list. |
| 207 | + |
| 208 | +If a particular symbol does not have a name (as is true of many of the |
| 209 | +more obscure symbols in the STIX fonts), Unicode characters can |
| 210 | +also be used:: |
| 211 | + |
| 212 | + ur'Generic symbol: $\u23ce$' |
| 213 | + |
| 214 | +Example |
| 215 | +------- |
| 216 | + |
| 217 | +Here is an example illustrating many of these features in context. |
| 218 | + |
| 219 | +.. literalinclude:: figures/pyplot_mathtext.py |
| 220 | + |
| 221 | +.. image:: figures/pyplot_mathtext.png |
| 222 | + :scale: 50 |
| 223 | + |
| 224 | + |
| 225 | + |
0 commit comments