title: plotting streamlines with python
body: <p>I am trying to plot streamlines and velocity potential for basic potential flows (uniform, source/sink, vortex, etc.)</p>
  
  <p>I am just starting out with python and so I'm a little confused. I am following <a href="https://tonysyu.github.io/plotting-streamlines-with-matplotlib-and-sympy.html#.WefpTIZrxE5" rel="nofollow noreferrer">this guide</a>.. </p>
  
  <p>I can plot the streamlines for the flow around a cylinder using this function</p>
  
  <pre><code>def cylinder_stream_function(U=1, R=1):
    r = sympy.sqrt(x**2 + y**2)
    theta = sympy.atan2(y, x)
    return U * (r - R**2 / r) * sympy.sin(theta)
  </code></pre>
  
  <p>and it works. But when I change the return statement to</p>
  
  <pre><code>return U * r * sympy.cos(theta)
  </code></pre>
  
  <p>for uniform flow I get the following error</p>
  
  <pre><code>Traceback (most recent call last):
     File "test.py", line 42, in
  &lt;module&gt;
      plot_streamlines(ax, u, v)
     File "test.py", line 32, in plot_streamlines
      ax.streamplot(X, Y, u(X, Y), v(X, Y), color='cornflowerblue')
     File "/usr/local/lib/python3.6/site-packages/matplotlib/__init__.py",
  line 1710, in inner
      return func(ax, *args, **kwargs)
     File "/usr/local/lib/python3.6/site-packages/matplotlib/axes/_axes.py",
  line 4688, in streamplot
      integration_direction=integration_direction)
     File "/usr/local/lib/python3.6/site-packages/matplotlib/streamplot.py",
  line 136, in streamplot
      if (u.shape != grid.shape) or (v.shape != grid.shape):
     AttributeError: 'int' object has no attribute 'shape'
  </code></pre>
  
  <p>I checked the type for the return object and it is <code>&lt;class 'sympy.core.mul.Mul'&gt;</code> with the first return statement and <code>&lt;class 'sympy.core.symbol.Symbol'&gt;</code> with the second one. Maybe this is related to why it doesn't work but I'm not sure how?</p>
  
  <p>I plot the streamlines with the following</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  import sympy
  from sympy.abc import x, y 
  
  def uniform_flow_stream_function(U=1):
    r = sympy.sqrt(x**2 + y**2)
    theta = sympy.atan2(y, x)
    return U * r * sympy.sin(theta)
  
  def velocity_field(psi):
    u = sympy.lambdify((x, y), psi.diff(y), 'numpy')
    v = sympy.lambdify((x, y), -psi.diff(x), 'numpy')
    return u, v
  
  def plot_streamlines(ax, u, v, xlim=(-4, 4), ylim=(-4, 4)):
      x0, x1 = xlim
      y0, y1 = ylim
      # create a grid of values
      Y, X =  np.ogrid[y0:y1:100j, x0:x1:100j]
      ax.streamplot(X, Y, u(X, Y), v(X, Y), color='cornflowerblue')
  
  psi = uniform_flow_stream_function()
  u, v = velocity_field(psi)
  fig, ax = plt.subplots(figsize=(5, 5))
  
  plot_streamlines(ax, u, v)
  plt.show()
  </code></pre>
  
  <p>Can someone please help me understand why this doesn't work and how I can get it to work? Thank you!</p>