Python中的极地热图

12

我想绘制一个二维极坐标热图,以抛物面 f(r)=r**2 为例。 我期望的输出结果是 enter image description here

我已经写好的代码如下

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

但是这个程序返回了以下图像。 在此输入图片描述

有人能帮忙吗?非常感谢。

1个回答

14

[编辑] 感谢 Александр Рах马耶夫

自版本3.3.3以来,默认情况下在pcolormesh中使用shading=flat会导致当前数据出错。我正在使用shading=closest。然后就不会出错了。例如:plt.pcolormesh(th, r, z, shading='nearest')也可参见此处


我认为您不小心混淆了radiuszenithazimuth :)

这将绘制我认为您想要的内容:

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

如果你想要射线网格线,你可以按照以下方法在每个Theta位置添加它们:

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

输入图像描述在此

还有更多类似这样的径向网格:

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

输入图像描述

PS:numpy和pyplot可使您的名称空间保持整洁...


1
自版本3.3.3起,shading=flat(默认情况下在pcolormesh中)的方法将对当前数据产生错误。 我正在使用shading=closest。然后就不会出现错误了。例如:plt.pcolormesh(th, r, z, shading='nearest')另请参见https://matplotlib.org/3.3.0/gallery/images_contours_and_fields/pcolormesh_grids.html。 - Alexander Rakhmaev
1
谢谢@АлександрРахмаев,我会在我的回答中包含您有用的评论。 - Reblochon Masque

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接