Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib mathtext \frac is too small

When I give matplotlib a annotation string such as

'$\frac{A}{B} = C$'

and I specify a fontsize of 18, the A and B are rendered at 12.6 pt, while the C is rendered at 18 pt. I want A, B, and C to all be the same size. How do I do this?

In a LaTeX document, if you give the commands

\begin{equation}
\frac{A}{B} = C
\end{equation}

you get a fraction, where A, B, and C are all the same size, but if you do

$\frac{A}{B} = C$

inline with text, you get the A and B rendered at 12.6 pt, while the C is rendered at 18 pt. Thus it appears matplotlib's mathtext is emulating LaTeX's inline mode. In LaTeX you can write

$\displaystyle\frac{A}{B} = C$

and then A, B, and C are all the same size, even in inline mode. I tried this in matplotlib, but mathtext did not recognize the command \displaystyle. =(

Is there a way to get this to work in Matplotlib's mathtext, or am I stuck changing text.usetex to true in my .matplotlibrc file? (If possible I would like to stay with mathtext since it is a lot faster.)

My setup: matplotlib v1.2.0 python 2.7 OS X 10.8

like image 327
Stretch Avatar asked Apr 07 '13 22:04

Stretch


People also ask

How do I increase legend size in Matplotlib?

The prop keyword is used to change the font size property. It is used in Matplotlib as Using a prop keyword for changing the font size in legend.

How do I write math in Matplotlib?

You can use a subset of TeX markup in any Matplotlib text string by placing it inside a pair of dollar signs ($). Note that you do not need to have TeX installed, since Matplotlib ships its own TeX expression parser, layout engine, and fonts.


2 Answers

For anyone stumbling on this and also not wanting to set "text.usetex" to True, matplotlib now supports the \dfrac macro (equivalent to \displaystyle\frac in LaTeX) to replace \frac since version 2.1

like image 60
Silmathoron Avatar answered Sep 21 '22 02:09

Silmathoron


As you said, you can fix it by using \displaystyle:

$\displaystyle\frac{A}{B} = C$

In order to allow matplotlib to use latex for all text handling you have to define in your matplotlibrc the text.usetex variable as True:

text.usetex          : True

I made a little example to verify it and it is working well:

import matplotlib.pyplot as plt
plt.plot(range(200))
plt.text(100,50,r'$\displaystyle\frac{A}{B}=C$')

I am sorry but since I am new, I can not post any image.

like image 37
Alejandro Avatar answered Sep 23 '22 02:09

Alejandro