首页 > 解决方案 > matplotlib 在极坐标图中颤动

问题描述

我想在 matplotlib 极坐标图中创建一个颤动图。我的 MWE 正在按预期工作rmin=0,如下图(左子图)所示,但对于rmin(右子图)的任何其他值都没有。

我还尝试计算极坐标中的端点并quiver()angles='xy'参数一起使用,但是由于某种原因这根本不起作用,我不明白为什么。但是,如果我将线图与计算的端点结合使用,它会按预期工作。

我更喜欢箭袋图而不是线图的原因是我想为情节设置动画并动态添加/删除点。对于 quiver 图,我可以通过设置偏移量和UV值来简单地做到这一点,对于线图,我必须添加/删除线图并结合set_data().

from matplotlib import pyplot as plt
import numpy as np

if __name__ == '__main__':
    N = 11  # number of samples
    x = np.linspace(0, 10, N)
    y = np.sin(x)

    theta, r = np.arctan2(y, x), np.hypot(x, y)  # convert to polar

    gradient = [np.ones(N), np.cos(x)]
    gradient = gradient / np.hypot(gradient[0], gradient[1])  # normalize

    endpoints_x = x + gradient[0]
    endpoints_y = y + gradient[1]

    # convert cartesian endpoints to polar
    endpoints_theta, endpoints_r = np.arctan2(endpoints_y, endpoints_x),\
                                   np.hypot(endpoints_x, endpoints_y)

    fig, axes = plt.subplots(1, 2, figsize=(10, 8), subplot_kw=dict(polar=True))

    for i in range(2):
        axes[i].scatter(theta, r, marker='o')

        axes[i].plot(np.stack((theta, endpoints_theta)), np.stack((r, endpoints_r)), 'r')

        # works only for rmin=0
        axes[i].quiver(theta, r, gradient[0], gradient[1], angles='uv')

        # does not work at all
        #axes[i].quiver(theta, r, endpoints_theta, endpoints_r, angles='xy')

        axes[i].set_thetamin(-90)
        axes[i].set_thetamax(90)

    axes[1].set_rmin(2)

    fig.tight_layout()
    plt.show()

极坐标图中的颤抖图

标签: pythonmatplotlibpolar-coordinates

解决方案


推荐阅读