Reputation: 41
Is it possible to show an equation from sympy in matplotlib? Let's suppose that I have this simple integral expression
How can I show the integral as shown in "Out [2]" in the xlabel of a graph in matplotlib?
Upvotes: 1
Views: 1958
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
Upvotes: 4