Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

Python中的极坐标热图

我想将抛物面f(r)= r ** 2绘制为二维极坐标热图。我期望的输出是在此处输入图片说明

我写的代码是

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

但是该程序返回以下图像。 在此处输入图片说明

有人可以帮忙吗?先感谢您。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
[edit]感谢АлександрРахмаев
从3.3.3版开始,shading=flat(pcolormesh默认情况下)方法将为当前数据提供错误。我正在使用 shanding=closest。这样就不会有错误。示例: plt.pcolormesh(th, r, z, shading='nearest') 另请参见

我想你在不经意间混合起来radius,zenith并azimuth:)

这说明了我认为您想要的:

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()
在此处输入图片说明

如果要使用射线网格线,可以按如下方式在每个Theta中添加它们:

plt.thetagrids([theta * 15 for theta in range(360//15)])
在此处输入图片说明

以及更多类似这样的径向网格:

plt.rgrids([.3 * _ for _ in range(1, 17)])
在此处输入图片说明

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

从版本3.3.3开始,shading = flat(默认为pcolormesh)方法将为当前数据提供错误。我正在使用shanding = closest。这样就不会有错误。示例: plt.pcolormesh(th, r, z, shading='nearest') 另请参见matplotlib.org/3.3.0/gallery/images_contours_and_fields/…。
谢谢@АлександрРахмаев,我将在您的回答中包含您的有用评论。

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...