Polar heatmaps in python

kanayamalakar picture kanayamalakar · Apr 9, 2016 · Viewed 8.5k times · Source

I want to plot a paraboloid f(r) = r**2 as a 2D polar heatmap. The output I expect is enter image description here

The code that I have written is

from pylab import*
from mpl_toolkits.mplot3d import Axes3D
ax = Axes3D(figure())
rad=linspace(0,5,100)
azm=linspace(0,2*pi,100)
r,th=meshgrid(rad,azm)
z=(r**2.0)/4.0
subplot(projection="polar")
pcolormesh(r,th, z)
show()

But this program returns the following image. enter image description here

Can someone help? Thank you in advance.

Answer

Reblochon Masque picture Reblochon Masque · Apr 9, 2016

[edit] thanks to Александр Рахмаев

Since version 3.3.3, the shading=flat (in pcolormesh by default) approach will give an error for the current data. I am using shanding=closest. Then there will be no error. Example: plt.pcolormesh(th, r, z, shading='nearest') See this also


I think you inadvertently mixed up radius, zenith and azimuth :)

This plots what I think you want:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

fig = plt.figure()
ax = Axes3D(fig)

rad = np.linspace(0, 5, 100)
azm = np.linspace(0, 2 * np.pi, 100)
r, th = np.meshgrid(rad, azm)
z = (r ** 2.0) / 4.0

plt.subplot(projection="polar")

plt.pcolormesh(th, r, z)
#plt.pcolormesh(th, z, r)

plt.plot(azm, r, color='k', ls='none') 
plt.grid()

plt.show()

enter image description here

If you want ray grid lines, you can add them every Theta as follows:

plt.thetagrids([theta * 15 for theta in range(360//15)])

enter image description here

and more radial grids like this:

plt.rgrids([.3 * _ for _ in range(1, 17)])

enter image description here

PS: numpy and pyplot will keep your namespace tidy...