76

My problem is I'd like to use Latex titles in some plots, and no latex in others. Right now, matplotlib has two different default fonts for Latex titles and non-Latex titles and I'd like the two to be consistent. Is there an RC setting I have to change that will allow this automatically?

I generate a plot with the following code:

import numpy as np
from matplotlib import pyplot as plt

tmpData = np.random.random( 300 )

##Create a plot with a tex title
ax = plt.subplot(211)
plt.plot(np.arange(300), tmpData)
plt.title(r'$W_y(\tau, j=3)$')
plt.setp(ax.get_xticklabels(), visible = False)

##Create another plot without a tex title
plt.subplot(212)
plt.plot(np.arange(300), tmpData )
plt.title(r'Some random numbers')
plt.show()

Here is the inconsistency I am talking about. The axis tick labels are thin looking relative to the titles.:

ncRubert
  • 3,822
  • 6
  • 25
  • 25
  • I believe [this answer](http://stackoverflow.com/questions/11611374/sans-serif-font-for-axes-tick-labels-with-latex) will help you – grg rsr May 02 '17 at 13:37

2 Answers2

80

To make the tex-style/mathtext text look like the regular text, you need to set the mathtext font to Bitstream Vera Sans,

import matplotlib
matplotlib.rcParams['mathtext.fontset'] = 'custom'
matplotlib.rcParams['mathtext.rm'] = 'Bitstream Vera Sans'
matplotlib.rcParams['mathtext.it'] = 'Bitstream Vera Sans:italic'
matplotlib.rcParams['mathtext.bf'] = 'Bitstream Vera Sans:bold'
matplotlib.pyplot.title(r'ABC123 vs $\mathrm{ABC123}^{123}$')

If you want the regular text to look like the mathtext text, you can change everything to Stix. This will affect labels, titles, ticks, etc.

import matplotlib
matplotlib.rcParams['mathtext.fontset'] = 'stix'
matplotlib.rcParams['font.family'] = 'STIXGeneral'
matplotlib.pyplot.title(r'ABC123 vs $\mathrm{ABC123}^{123}$')

Basic idea is that you need to set both the regular and mathtext fonts to be the same, and the method of doing so is a bit obscure. You can see a list of the custom fonts,

sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])

As others mentioned, you can also have Latex render everything for you with one font by setting text.usetex in the rcParams, but that's slow and not entirely necessary.

lindyblackburn
  • 1,655
  • 16
  • 9
  • 15
    Nice job! with the "stix" options it works fine; however the most nice LaTeX result is by changing `matplotlib.rcParams['mathtext.fontset'] = 'stix'` into `matplotlib.rcParams['mathtext.fontset'] = 'cm'`. This is "[computer modern](https://en.wikipedia.org/wiki/Computer_Modern)" the font of LaTeX. Eventually changing the family into `matplotlib.rcParams['font.family'] = 'cmu serif'` may work, but in my case I had problem with the minus sign, therefore I changed `'stix'` into `'cm'` but I left `'STIXGeneral'` and it looks nice ;) – loved.by.Jesus Jul 10 '18 at 19:00
  • 1
    @loved.by.Jesus: Is it possible to use `cm` for x ticks and y ticks by default? – s.ouchene Apr 15 '19 at 02:34
19

EDIT

if you want to change the fonts used by LaTeX inside matplotlib, check out this page

http://matplotlib.sourceforge.net/users/usetex.html

one of the examples there is

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

Just pick your favorite!

And if you want a bold font, you can try \mathbf

plt.title(r'$\mathbf{W_y(\tau, j=3)}$')

EDIT 2

The following will make bold font default for you

font = {'family' : 'monospace',
        'weight' : 'bold',
        'size'   : 22}

rc('font', **font)
nye17
  • 12,857
  • 11
  • 58
  • 68
  • 1
    I think I should clarify my question. Right now, I think the Latex font is the uglier one and I'd like to bring it in line with the non-latex font. I'd like something thicker so my plot text is still readable if the plots are shrunk down and put in a thesis/publication. – ncRubert Jul 06 '12 at 18:44
  • Another clarification. I want the axis labels made thicker. They're the most unreadable part of the graph. I updated the question to reflect this. – ncRubert Jul 06 '12 at 19:11
  • @ncRubert the `rc` settings are global, it'll affect all the fonts in the plot. – nye17 Jul 06 '12 at 19:50
  • 4
    Is there an automatic way to make the axis tick labels default to bold face? I want to make the labels taller, wider, and the individual characters thicker. – ncRubert Jul 06 '12 at 19:56
  • You should increase font size if the plot are shrunk so that they are consistent with your text. Admitedly, matplotlib can make this cumbersome. – M. Toya Apr 17 '13 at 09:42
  • I get `RuntimeError: LaTeX was not able to process the following string: 'lp'`. Do I need to install LaTeX first on my machine (OSX)? – n1000 Jan 27 '15 at 08:16