Skip to content
Advertisement

Displaying sympy equations on matplot

import matplotlib.pyplot as plt
import sympy as sp
import numpy as np

q = [3,5]
t,i = sp.symbols('t,i')
eq = t**2/(q[1]-(t/q[0])**2)
x = np.linspace(0,100,10000)
y=x**2
plt.plot(x,y)

Now I want to display the sympy equation “eq” on the plot. I have seen many methods to display the same equation using tex commands, but I specifically want to display the sympy eq. Thanks in advance

Advertisement

Answer

You could use sympy’s latex() function like this:

import matplotlib.pyplot as plt
import sympy as sp
import numpy as np

q = [3,5]
t,i = sp.symbols('t,i')
eq = t**2/(q[1]-(t/q[0])**2)
x = np.linspace(0,100,10000)
y=x**2  
plt.plot(x,y, label=r"$" + f"{sp.latex(eq)}" + r"$")
plt.legend()
plt.show()
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement