Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the benefit of text.usetex : True in matplotlib

I'm intending to write a thesis and am beginning by setting up a standard Matplotlib file to control plot formatting. However, I'm having problems with the text.usetex : True option. In particular, it's irritating that the tick labels default to a serif font when all my figures should be sans-serif. Indeed - I set the font.family to sans-serif in the rcParams file but still see the problem, as identified in github here.

Additionally, other text looks different when I have usetex turned on or off - this seems surprising since I told matplotlib to use the same font each time.

Therefore, I wonder what the actual benefit to using LaTeX rendering is? Since Matplotlib can already handle LaTeX commands in labels such as xlabel('\alpha') and can accept fonts to use by user input to the rcparams file, what does using LaTeX on the text do differently?

To achieve my aim of a consistent sans-serif font, could I not just set font.sans-serif in matplotlib rcparams file to be the font I set as a sans-serif font in LaTeX?

Thanks for any suggestions or hints!

like image 369
user3234139 Avatar asked Jan 25 '14 02:01

user3234139


2 Answers

The usetex setting is particularly useful when you need LaTeX features that aren't present in matplotlib's built-in mathtext. But it also provides somewhat better typography, and you don't have to worry about parts of mathtext that are nonstandard.

If you compare the two examples below (based on the example at the end of this page), you will see that the LaTeX version does a better job with the math, particularly the summation. Also, mathtext doesn't know about \displaystyle but uses that style of layout automatically, which could be undesirable in some circumstances.

In terms of your issues with the tick label fonts, I believe that matplotlib is using the default LaTeX math font for labels. If you try something along the lines of the code I commented out in the second example, you should be able to get what you want.

If you are only making relatively simple plots, you should take a look at tikzplotlib. It allows you to save figures in a tikz format that allows easy resizing. See my answer to this question for more details.


mathtext version

import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2*np.pi*t)

plt.plot(t,s)
plt.title(r'$\alpha_i > \beta_i$', fontsize=20)
plt.text(1, -0.6, r'$\sum_{i=0}^\infty x_i$', fontsize=20)
plt.text(0.6, 0.6, r'$\mathcal{A}\mathrm{sin}(2 \omega t)$',
         fontsize=20)
plt.xlabel('time (s)')
plt.ylabel('volts (mV)')
plt.savefig('fig_mathtext.pdf')

enter image description here

LaTeX version

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
rc('text', usetex=True)
#rc('text.latex', preamble=r'\usepackage[eulergreek]{sansmath}\sansmath')

t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2*np.pi*t)

figure()

plt.plot(t,s)
plt.title(r'$\alpha_i > \beta_i$', fontsize=20)
plt.text(1, -0.6, r'$\displaystyle\sum_{i=0}^\infty x_i$', fontsize=20)
plt.text(0.6, 0.6, r'$\mathcal{A}\mathrm{sin}(2 \omega t)$',
         fontsize=20)
plt.xlabel('time (s)')
plt.ylabel('volts (mV)')
plt.savefig('fig_latex.pdf')

enter image description here

like image 93
G. Poore Avatar answered Oct 24 '22 02:10

G. Poore


The benefits of using usetex include typesetting complex formulas that matplotlib's built-in engine doesn't support, and the ability to include an arbitrary preamble using the (officially unsupported) text.latex.preamble setting. The latter can be useful if you are embedding the matplotlib figure in a LaTeX document and want to match the fonts exactly, e.g. because of a journal stylesheet. But for most uses I would recommend the built-in mathtext renderer.

like image 40
Jouni K. Seppänen Avatar answered Oct 24 '22 00:10

Jouni K. Seppänen