2

When executing the following code in python using jupyter/ipython, the Latex symbols are not displayed correctly. Any Idea why this could be?

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

x = np.linspace(0,3)
y = np.sin(x)
plt.plot(x,y)
plt.title(r'$\beta \rho \lambda \xi$',fontsize=30)

Output:

enter image description here

4
  • 1
    cannot reproduce. Can you add your matplotlib version? Commented Jul 7, 2016 at 7:35
  • Matplotlib is version 1.5.2rc2, jupyter version 4.1.0 Commented Jul 7, 2016 at 7:42
  • Hmh, 1.5.2 seems to work for me as well. Maybe a simple reinstall will do? Commented Jul 7, 2016 at 9:29
  • Probably yes, I will try that later Commented Jul 7, 2016 at 15:00

1 Answer 1

2

IPython notebook uses MathJax to render LaTeX inside html/markdown. Just put your LaTeX math inside $$.

$$c = \sqrt{a^2 + b^2}$$

enter image description here

You have to write :

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,3)
y = np.sin(x)
plt.plot(x,y)
plt.title(r'$$\beta \rho \lambda \xi$$',fontsize=30)

plt.show()

--------------------

Or you can display LaTeX / Math output from Python, as seen towards the end of the notebook tour :

from IPython.display import display, Math, Latex
display(Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx'))

enter image description here

---------------------

Other kind of solution :

import numpy as np
from matplotlib import pyplot as plt
import matplotlib as mpl
mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.preamble'] = [r'\usepackage{amsmath}'] #for \text command

x = np.linspace(0,3)
y = np.sin(x)
plt.plot(x,y)
plt.title(r'$text{\beta \rho \lambda \xi}$',fontsize=30)

plt.show()
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, but this gives me the error "TclError: no display name and no $DISPLAY environment variable". Seems like I cannot place double $'s
I did not know that, that's actually very helpful. (+1) However, it does not solve my original problem as far as I can see. (Making matplotlib labels/titles with LaTex in python).
@physicsGuy I don't work on jupiter/notebook but I knew that this solution worked. I will see with my jupiter friend user if he has an other solution ;)
Maybe other solution : plt.title(r"\TeX\ is Number " r"$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!", fontsize=16, color='gray')
It seems as something in my system is not up to date, as I get the error "ParseFatalException: Unknown symbol: \displaystyle (at char 0), (line:1, col:1)", when using your line.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.