动画极坐标图

发布于 2025-01-15 23:24:02 字数 810 浏览 3 评论 0原文

我正在尝试使用 python 制作行星运动的动画图。当我没有动画时,我可以获得正确的路径显示,但是一旦我尝试对其进行动画处理,它就会显示为空白,即使当我尝试在控制台中打印每个值时, r 和 i 被输出为正确的值。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig = plt.figure(figsize=(6,6))
ax = plt.subplot(111, polar=True)
ax.set_ylim(0,1)
line, = ax.plot([],[])

semi_major_axis = 1
eccentricity = 0.1
theta = np.linspace(0,2*np.pi, num = 50)

point, = ax.plot(0,1, marker="o")

def frame(i):
    r=(semi_major_axis*(1-eccentricity**2)/(1-eccentricity*np.cos(i)))
    line.set_xdata(i)
    line.set_ydata(r)
    return line,


ax.set_rmax(semi_major_axis+1)
ax.set_rticks(np.linspace(0,semi_major_axis+1, num = 5))
ax.set_rlabel_position(-22.5) 


animation = FuncAnimation(fig, func=frame, frames=theta, interval=10)
plt.show()

I am trying to make an animated plot of planetary motion using python. I can get the correct path to show up when not animated but once I try to animate it, it just shows up blank even though r and i are being output as the correct values when I try to print each value in the console.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig = plt.figure(figsize=(6,6))
ax = plt.subplot(111, polar=True)
ax.set_ylim(0,1)
line, = ax.plot([],[])

semi_major_axis = 1
eccentricity = 0.1
theta = np.linspace(0,2*np.pi, num = 50)

point, = ax.plot(0,1, marker="o")

def frame(i):
    r=(semi_major_axis*(1-eccentricity**2)/(1-eccentricity*np.cos(i)))
    line.set_xdata(i)
    line.set_ydata(r)
    return line,


ax.set_rmax(semi_major_axis+1)
ax.set_rticks(np.linspace(0,semi_major_axis+1, num = 5))
ax.set_rlabel_position(-22.5) 


animation = FuncAnimation(fig, func=frame, frames=theta, interval=10)
plt.show()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城孤痞 2025-01-22 23:24:02

我认为问题是你正在绘制一个点而不是一条线。以下代码对我有用:

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.animation import FuncAnimation

    fig = plt.figure(figsize=(6, 6))
    ax = plt.subplot(111, polar=True)
    ax.set_ylim(0, 1)

    semi_major_axis = 1
    eccentricity = 0.1
    theta = np.linspace(0, 2 * np.pi, num=50)

    def frame(i):
        r = (semi_major_axis * (1 - eccentricity ** 2) / (1 - eccentricity * np.cos(i)))
        ax.plot(i,r,'b', marker='o')

    ax.set_rmax(semi_major_axis + 1)
    ax.set_rticks(np.linspace(0, semi_major_axis + 1, num=5))
    ax.set_rlabel_position(-22.5)

    animation = FuncAnimation(fig, func=frame, frames=theta, interval=10)
    plt.show()

I think the problem is you are plotting a point and not a line. The following code worked for me:

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.animation import FuncAnimation

    fig = plt.figure(figsize=(6, 6))
    ax = plt.subplot(111, polar=True)
    ax.set_ylim(0, 1)

    semi_major_axis = 1
    eccentricity = 0.1
    theta = np.linspace(0, 2 * np.pi, num=50)

    def frame(i):
        r = (semi_major_axis * (1 - eccentricity ** 2) / (1 - eccentricity * np.cos(i)))
        ax.plot(i,r,'b', marker='o')

    ax.set_rmax(semi_major_axis + 1)
    ax.set_rticks(np.linspace(0, semi_major_axis + 1, num=5))
    ax.set_rlabel_position(-22.5)

    animation = FuncAnimation(fig, func=frame, frames=theta, interval=10)
    plt.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文