【问题标题】:Polar heatmaps in pythonpython中的极地热图
【发布时间】:2016-07-30 12:38:19
【问题描述】:

我想将抛物面 f(r) = r**2 绘制为 2D 极坐标热图。我期望的输出是

我写的代码是

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

但是这个程序返回下面的图像。

有人可以帮忙吗?提前谢谢你。

【问题讨论】:

    标签: python python-3.x matplotlib heatmap polar-coordinates


    【解决方案1】:

    [编辑] 感谢 Александр Рахмаев

    自 3.3.3 版起,shading=flat(默认为 pcolormesh) 方法将对当前数据给出错误。我在用 shanding=closest。那么就不会出错了。例子: plt.pcolormesh(th, r, z, shading='nearest')See this also


    我认为你无意中混淆了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()
    

    如果你想要射线网格线,你可以按如下方式在每个 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/…
    • 谢谢@АлександрРахмаев ,我将在我的回答中包含您的有用评论。
    猜你喜欢
    • 1970-01-01
    • 2015-10-05
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 2018-09-12
    相关资源
    最近更新 更多