What is the use of text.usetex: True in matplotlib

I am going to write a dissertation and start by creating a standard Matplotlib file to control graph formatting. However, I am having problems with the text.usetex : True parameter. In particular, it is annoying that tick marks have a serif font by default when all my digits should be sans-serif. Indeed - I installed font.family in sans-serif in the rcParams file, but still see the problem as stated on github here .

Also, other text looks different when I turn usetex on or off - this seems awesome since I told matplotlib to use the same font every time.

Therefore, I wonder what the actual benefits of using LaTeX rendering are. Since Matplotlib can already handle LaTeX commands in methods like xlabel('\alpha') , and can accept fonts that will be used when the user enters the rcparams file, what makes using LaTeX different in the text?

To achieve the goal of a consistent sans-serif font, can I not just install font.sans-serif in the matplotlib rcparams file with the font that I set as the sans-serif font in LaTeX?

Thanks for any suggestions or tips!

+7
python matplotlib latex
source share
2 answers

The usetex parameter usetex especially useful when you need LaTeX functions that are not in the matplotlib inline math text. But it also provides slightly better typography, and you don't need to worry about non-standard parts of the math text.

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 of math, especially summation. In addition, mathtext does not know about \displaystyle but uses this layout style automatically, which may be undesirable in some circumstances.

Regarding your problems with label fonts, I believe matplotlib uses the default LaTeX math font for labels. If you try something similar to the code that I commented in the second example, you can get what you want.

If you only make relatively simple stories, you should take a look at tikzplotlib . This allows you to save shapes in tikz format, making it easy to resize. See my answer to this question for more details.


mathematical 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

+4
source share

The benefits of using usetex include a set of complex formulas that the matplotlib engine does not support, and the ability to include an arbitrary preamble using the (officially unsupported) parameter text.latex.preamble . The latter can be useful if you insert a matplotlib shape into a LaTeX document and want to exactly match the fonts, for example. because of the style sheet of the magazine. But for most applications, I would recommend mathtext's built-in rendering.

+2
source share

All Articles