title: How to plot grad(f(x,y))?
body: <p>I want to calculate and plot a gradient of any scalar function of two variables. If you really want a concrete example, lets say f=x^2+y^2 where x goes from -10 to 10 and same for y. How do I calculate and plot grad(f)? The solution should be vector and I should see vector lines. I am new to python so please use simple words.</p>
  
  <p>EDIT:</p>
  
  <p>@Andras Deak: thank you for your post, i tried what you suggested and instead of your test function (fun=3*x^2-5*y^2) I used function that i defined as V(x,y); this is how the code looks like but it reports an error</p>
  
  <pre><code>import numpy as np
  import math 
  import sympy
  import matplotlib.pyplot as plt
  
  def V(x,y):
      t=[]
      for k in range (1,3): 
          for l in range (1,3):
              t.append(0.000001*np.sin(2*math.pi*k*0.5)/((4*(math.pi)**2)* (k**2+l**2)))  
              term = t* np.sin(2 * math.pi * k * x/0.004) * np.cos(2 * math.pi * l * y/0.004)
              return term 
      return term.sum()
  
  x,y=sympy.symbols('x y')
  fun=V(x,y)
  gradfun=[sympy.diff(fun,var) for var in (x,y)]
  numgradfun=sympy.lambdify([x,y],gradfun)
  
  X,Y=np.meshgrid(np.arange(-10,11),np.arange(-10,11))
  graddat=numgradfun(X,Y)
  plt.figure()
  plt.quiver(X,Y,graddat[0],graddat[1])
  plt.show()
  
  AttributeError: 'Mul' object has no attribute 'sin'
  </code></pre>
  
  <p>And lets say I remove sin, I get another error:</p>
  
  <pre><code>TypeError: can't multiply sequence by non-int of type 'Mul'
  </code></pre>
  
  <p>I read tutorial for sympy and it says "The real power of a symbolic computation system such as SymPy is the ability to do all sorts of computations symbolically". I get this, I just dont get why I cannot multiply x and y symbols with float numbers.</p>
  
  <p>What is the way around this? :( Help please! </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>@Andras Deak: I wanted to make things shorter so I removed many constants from the original formulas for V(x,y) and Cn*Dm. As you pointed out, that caused the sin function to always return 0 (i just noticed). Apologies for that. I will update the post later today when i read your comment in details. Big thanks! </p>
  
  <p><strong>UPDATE 2</strong>
  I changed coefficients in my expression for voltage and this is the result:</p>
  
  <p><a href="https://i.stack.imgur.com/FlDLH.png" rel="noreferrer"><img src="https://i.stack.imgur.com/FlDLH.png" alt="plot"></a></p>
  
  <p>It looks good except that the arrows point in the opposite direction (they are supposed to go out of the reddish dot and into the blue one). Do you know how I could change that? And if possible, could you please tell me the way to increase the size of the arrows? I tried what was suggested in another topic (<a href="https://stackoverflow.com/questions/25342072/computing-and-drawing-vector-fields">Computing and drawing vector fields</a>):</p>
  
  <pre><code>skip = (slice(None, None, 3), slice(None, None, 3))
  </code></pre>
  
  <p>This plots only every third arrow and matplotlib does the autoscale but it doesnt work for me (nothing happens when i add this, for any number that i enter)
  You were already of huge help , i cannot thank you enough!</p>