|
| 1 | +""" |
| 2 | +================================= |
| 3 | +Rendering math equation using TeX |
| 4 | +================================= |
| 5 | +
|
| 6 | +You can use TeX to render all of your matplotlib text if the rc |
| 7 | +parameter text.usetex is set. This works currently on the agg and ps |
| 8 | +backends, and requires that you have tex and the other dependencies |
| 9 | +described at http://matplotlib.org/users/usetex.html |
| 10 | +properly installed on your system. The first time you run a script |
| 11 | +you will see a lot of output from tex and associated tools. The next |
| 12 | +time, the run may be silent, as a lot of the information is cached. |
| 13 | +
|
| 14 | +Notice how the the label for the y axis is provided using unicode! |
| 15 | +
|
| 16 | +""" |
| 17 | +from __future__ import unicode_literals |
| 18 | +import numpy as np |
| 19 | +import matplotlib |
| 20 | +matplotlib.rcParams['text.usetex'] = True |
| 21 | +matplotlib.rcParams['text.latex.unicode'] = True |
| 22 | +import matplotlib.pyplot as plt |
| 23 | + |
| 24 | + |
| 25 | +t = np.linspace(0.0, 1.0, 100) |
| 26 | +s = np.cos(4 * np.pi * t) + 2 |
| 27 | + |
| 28 | +fig, ax = plt.subplots(figsize=(6, 4), tight_layout=True) |
| 29 | +ax.plot(t, s) |
| 30 | + |
| 31 | +ax.set_xlabel(r'\textbf{time (s)}') |
| 32 | +ax.set_ylabel('\\textit{Velocity (\N{DEGREE SIGN}/sec)}', fontsize=16) |
| 33 | +ax.set_title(r'\TeX\ is Number $\displaystyle\sum_{n=1}^\infty' |
| 34 | + r'\frac{-e^{i\pi}}{2^n}$!', fontsize=16, color='r') |
| 35 | +plt.show() |
0 commit comments