This is an archival dump of old wiki content --- see scipy.org for current material.
Please see http://scipy-cookbook.readthedocs.org/

Matplotlib can use LaTeX to handle the text layout in your figures. This option (which is still somewhat experimental) can be activated by setting text.usetex : true in your rc settings. Text handling with matplotlib's LaTeX support is slower than standard text handling, but is more flexible, and produces publication-quality plots. The results are striking, especially when you take care to use the same fonts in your figures as in the main document.

Matplotlib's LaTeX support is still under development, although at least two individuals have relied upon it to generate the figures for their doctoral dissertations. Many improvements have been made beginning with matplotlib-0.87, please update matplotlib if you have an earlier version. This option requires a working LaTeX installation, dvipng (which may be included with your TeX installation), and ghostscript (AFPL, GPL, or ESP ghostscript should all work, but GPL ghostscript-8.60 or later is recommended). The executables for these external dependencies must be located on your PATH.

There are a couple of options to mention, which can be changed using rc settings, either using a matplotlibrc file, or the rcParams dict in your program. Here is an example matplotlibrc file:

font.family        : serif
font.serif         : Times, Palatino, New Century Schoolbook, Bookman, Computer Modern Roman
font.sans-serif    : Helvetica, Avant Garde, Computer Modern Sans serif
font.cursive       : Zapf Chancery
font.monospace     : Courier, Computer Modern Typewriter

text.usetex        : true

The first valid font in each family is the one that will be loaded. If the fonts are not specified, the Computer Modern fonts are used by default. All of the other fonts are Adobe fonts. Times and Palatino each have their own accompanying math fonts, while the other Adobe serif fonts make use of the Computer Modern math fonts. See psnfss2e.pdf for more details.

To use tex and select e.g. Helvetica as the default font, without edititing matplotlibrc use:

   1 from matplotlib import rc
   2 rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
   3 ## for Palatino and other serif fonts use:
   4 #rc('font',**{'family':'serif','serif':['Palatino']})
   5 rc('text', usetex=True)

N.B. You need to do this before you import matplotlib.pylab.

Here is the standard example, tex_demo.py:

   1 from matplotlib import rc
   2 from matplotlib.numerix import arange, cos, pi
   3 from pylab import figure, axes, plot, xlabel, ylabel, title, grid, savefig, show
   4 
   5 rc('text', usetex=True)
   6 figure(1)
   7 ax = axes([0.1, 0.1, 0.8, 0.7])
   8 t = arange(0.0, 1.0+0.01, 0.01)
   9 s = cos(2*2*pi*t)+2
  10 plot(t, s)
  11 
  12 xlabel(r'\textbf{time (s)}')
  13 ylabel(r'\textit{voltage (mV)}',fontsize=16)
  14 title(r"\TeX\ is Number $\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!", fontsize=16, color='r')
  15 grid(True)
  16 savefig('tex_demo')
  17 
  18 show()

tex_demo.png

Note that when TeX/LaTeX support is enabled, you can mix text and math modes. Display math mode ($$ e=mc^2 $$) is not supported, but adding the command \displaystyle, as in tex_demo.py, will produce the same results.

In order to produce encapsulated postscript files that can be embedded in a new LaTeX document, the default behavior of matplotlib is to distill the output, which removes some postscript operators used by LaTeX that are illegal in an eps file. This step produces fonts which may be unacceptable to some users. One workaround is to to set ps.distiller.res to a higher value (perhaps 6000) in your rc settings. A better workaround, which requires xpdf or poppler (the new backend to xpdf) can be activated by changing the rc ps.usedistiller setting to xpdf. The xpdf alternative produces postscript with text that can be edited in Adobe Illustrator, or searched for once converted to pdf.

Possible Hangups

In the event that things dont work


CategoryCookbookMatplotlib

SciPy: Cookbook/Matplotlib/UsingTex (last edited 2015-10-24 17:48:25 by anonymous)