Polar heatmaps in python

瘦欲@ 提交于 2019-12-18 16:41:25

问题


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

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.

Can someone help? Thank you in advance.


回答1:


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()

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

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

and more radial grids like this:

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

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



来源:https://stackoverflow.com/questions/36513312/polar-heatmaps-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!