|
1 |
| -#!/usr/bin/env python |
2 | 1 | """
|
| 2 | +Demo of TeX rendering. |
| 3 | +
|
3 | 4 | You can use TeX to render all of your matplotlib text if the rc
|
4 | 5 | parameter text.usetex is set. This works currently on the agg and ps
|
5 | 6 | backends, and requires that you have tex and the other dependencies
|
|
10 | 11 | ~/.tex.cache
|
11 | 12 |
|
12 | 13 | """
|
13 |
| -from matplotlib import rc |
14 |
| -from numpy import arange, cos, pi |
15 |
| -from matplotlib.pyplot import figure, axes, plot, xlabel, ylabel, title, \ |
16 |
| - grid, savefig, show |
| 14 | +import numpy as np |
| 15 | +import matplotlib.pyplot as plt |
17 | 16 |
|
18 | 17 |
|
19 |
| -rc('text', usetex=True) |
20 |
| -rc('font', family='serif') |
21 |
| -figure(1, figsize=(6,4)) |
22 |
| -ax = axes([0.1, 0.1, 0.8, 0.7]) |
23 |
| -t = arange(0.0, 1.0+0.01, 0.01) |
24 |
| -s = cos(2*2*pi*t)+2 |
25 |
| -plot(t, s) |
| 18 | +# Example data |
| 19 | +t = np.arange(0.0, 1.0 + 0.01, 0.01) |
| 20 | +s = np.cos(4 * np.pi * t) + 2 |
26 | 21 |
|
27 |
| -xlabel(r'\textbf{time (s)}') |
28 |
| -ylabel(r'\textit{voltage (mV)}',fontsize=16) |
29 |
| -title(r"\TeX\ is Number $\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!", |
30 |
| - fontsize=16, color='r') |
31 |
| -grid(True) |
32 |
| -savefig('tex_demo') |
| 22 | +plt.rc('text', usetex=True) |
| 23 | +plt.rc('font', family='serif') |
| 24 | +plt.plot(t, s) |
33 | 25 |
|
| 26 | +plt.xlabel(r'\textbf{time} (s)') |
| 27 | +plt.ylabel(r'\textit{voltage} (mV)',fontsize=16) |
| 28 | +plt.title(r"\TeX\ is Number " |
| 29 | + r"$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!", |
| 30 | + fontsize=16, color='gray') |
| 31 | +# Make room for the ridiculously large title. |
| 32 | +plt.subplots_adjust(top=0.8) |
34 | 33 |
|
35 |
| -show() |
| 34 | +plt.savefig('tex_demo') |
| 35 | +plt.show() |
0 commit comments