|
1 | 1 | """
|
2 |
| -======== |
3 |
| -Tex Demo |
4 |
| -======== |
| 2 | +================================= |
| 3 | +Rendering math equation using TeX |
| 4 | +================================= |
5 | 5 |
|
6 | 6 | You can use TeX to render all of your matplotlib text if the rc
|
7 | 7 | parameter text.usetex is set. This works currently on the agg and ps
|
8 | 8 | backends, and requires that you have tex and the other dependencies
|
9 | 9 | described at http://matplotlib.org/users/usetex.html
|
10 | 10 | properly installed on your system. The first time you run a script
|
11 | 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 in |
13 |
| -~/.tex.cache |
| 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! |
14 | 15 |
|
15 | 16 | """
|
| 17 | +from __future__ import unicode_literals |
16 | 18 | import numpy as np
|
| 19 | +import matplotlib |
| 20 | +matplotlib.rcParams['text.usetex'] = True |
| 21 | +matplotlib.rcParams['text.latex.unicode'] = True |
17 | 22 | import matplotlib.pyplot as plt
|
18 | 23 |
|
19 | 24 |
|
20 |
| -plt.rc('text', usetex=True) |
21 |
| -plt.rc('font', family='serif') |
22 |
| -plt.figure(1, figsize=(6, 4)) |
23 |
| -ax = plt.axes([0.1, 0.1, 0.8, 0.7]) |
24 | 25 | t = np.linspace(0.0, 1.0, 100)
|
25 | 26 | s = np.cos(4 * np.pi * t) + 2
|
26 |
| -plt.plot(t, s) |
27 | 27 |
|
28 |
| -plt.xlabel(r'\textbf{time (s)}') |
29 |
| -plt.ylabel(r'\textit{voltage (mV)}', fontsize=16) |
30 |
| -plt.title(r"\TeX\ is Number $\displaystyle\sum_{n=1}^\infty" |
31 |
| - r"\frac{-e^{i\pi}}{2^n}$!", fontsize=16, color='r') |
32 |
| -plt.grid(True) |
33 |
| -plt.savefig('tex_demo') |
| 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') |
34 | 35 | plt.show()
|
0 commit comments