I wrote some code to use sympy to find the gradient of a function f(x,y) = x*y**2, and then to plot the vector field from the gradient. See Below:
%matplotlib inline
import matplotlib.pyplot as plt
import sympy as sp
import numpy as np
sp.init_printing()
x,y = sp.symbols('x y')
def gradient(f):
return (f.diff(x), f.diff(y))
f = x*y**2
g = gradient(f)
g
X,Y = np.meshgrid(np.linspace(-3,3,15), np.linspace(-3,3,15))
U=[g[0].subs({x:x1, y:y1}) for (x1,y1) in zip(X,Y)]
V=[g[1].subs({x:x1, y:y1}) for (x1,y1) in zip(X,Y)]
plt.quiver(X,Y,U,V, linewidth=1)
plt.title("vector field")
plt.show()
What i'm wondering about is why the sympy "subs" function is not working in this code. Its just returns the expression without inserting a value of X and Y to evaluate to a numerical value, instead its just returning the sympy object without any subsitution.