Matplotlib - contour and quiver plot in projected polar coordinates
<p>I need to plot contour and quiver plots of scalar and vector fields  defined on an uneven grid in (r,theta) coordinates.</p>

<p>As a minimal example of the problem I have, consider the contour plot of a <em><a href="https://en.wikipedia.org/wiki/Stream_function" rel="nofollow">Stream function</a></em> for a magnetic dipole, contours of such a function are streamlines of the corresponeding vector field (in this case, the magnetic field). </p>

<p>The code below takes an uneven grid in (r,theta) coordinates, maps it to the cartesian plane and plots a contour plot of the stream function.</p>

<pre><code>import numpy as np
import matplotlib.pyplot as plt

r = np.logspace(0,1,200)
theta = np.linspace(0,np.pi/2,100)

N_r = len(r)
N_theta = len(theta)

# Polar to cartesian coordinates
theta_matrix, r_matrix = np.meshgrid(theta, r)
x = r_matrix * np.cos(theta_matrix)
y = r_matrix * np.sin(theta_matrix)

m = 5
psi = np.zeros((N_r, N_theta))

# Stream function for a magnetic dipole
psi = m * np.sin(theta_matrix)**2 / r_matrix

contour_levels = m * np.sin(np.linspace(0, np.pi/2,40))**2.

fig, ax = plt.subplots()
# ax.plot(x,y,'b.')  # plot grid points
ax.set_aspect('equal')
ax.contour(x, y, psi, 100, colors='black',levels=contour_levels)
plt.show()
</code></pre>

<p>For some reason though, the plot I get doesn't look right:
<a href="http://i.stack.imgur.com/v0nxU.png" rel="nofollow"><img src="http://i.stack.imgur.com/v0nxU.png" alt="Contour plot of a stream function for a magnetic dipole."></a></p>

<p>If I interchange x and y in the contour function call, I get the desired result:
<a href="http://i.stack.imgur.com/MPRX0.png" rel="nofollow"><img src="http://i.stack.imgur.com/MPRX0.png" alt="enter image description here"></a></p>

<p>Same thing happens when I try to make a quiver plot of a <em>vector field</em> defined on the same grid and mapped to the x-y plane, except that interchanging x and y in the function call no longer works. </p>

<p>It seems like I made a stupid mistake somewhere but I can't figure out what it is.</p>

<p>If <code>psi = m * np.sin(theta_matrix)**2 / r_matrix</code>
then psi increases as theta goes from 0 to pi/2 and psi decreases as r increases.</p>

<p>So a contour line for psi should increase in r as theta increases. That results
in a curve that goes counterclockwise as it radiates out from the center. This is
consistent with the first plot you posted, and the result returned by the first version of your code with</p>

<pre><code>ax.contour(x, y, psi, 100, colors='black',levels=contour_levels)
</code></pre>

<hr>

<p>An alternative way to confirm the plausibility of the result is to look at a surface plot of <code>psi</code>:</p>

<pre><code>import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d

r = np.logspace(0,1,200)
theta = np.linspace(0,np.pi/2,100)

N_r = len(r)
N_theta = len(theta)

# Polar to cartesian coordinates
theta_matrix, r_matrix = np.meshgrid(theta, r)
x = r_matrix * np.cos(theta_matrix)
y = r_matrix * np.sin(theta_matrix)

m = 5

# Stream function for a magnetic dipole
psi = m * np.sin(theta_matrix)**2 / r_matrix

contour_levels = m * np.sin(np.linspace(0, np.pi/2,40))**2.

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
ax.set_aspect('equal')

ax.plot_surface(x, y, psi, rstride=8, cstride=8, alpha=0.3)
ax.contour(x, y, psi, colors='black',levels=contour_levels)
plt.show()
</code></pre>

<p><a href="http://i.stack.imgur.com/E4IjS.png" rel="nofollow"><img src="http://i.stack.imgur.com/E4IjS.png" alt="enter image description here"></a></p>

python numpy matplotlib contour polar-coordinates