Raphael Vico
Raphael Vico

Reputation: 41

How to show a sympy equation in matplotlib

Is it possible to show an equation from sympy in matplotlib? Let's suppose that I have this simple integral expression

1

How can I show the integral as shown in "Out [2]" in the xlabel of a graph in matplotlib?

Upvotes: 1

Views: 1958

Answers (1)

Reti43
Reti43

Reputation: 9796

Yes, it can be done. Matplotlib supports TeX for text by surrounding specific text syntax between dollar signs. And sympy can export an expression to latex syntax, so you don't have to do any manual parsing.

import matplotlib.pyplot as plt
import sympy as sym
from sympy.printing import latex
    
x = sym.symbols('x')
f = sym.Integral(sym.sqrt(1/x), x)
plt.plot([])
plt.xlabel(f'${latex(f)}$')
plt.show()

Output

1

Upvotes: 4

Related Questions