【问题标题】:How do I plot an animation and a point in the same matplotlib plot如何在同一个 matplotlib 图中绘制动画和点
【发布时间】:2020-12-07 13:28:35
【问题描述】:

我已经创建了 x_graphy_graph 的动画情节,它绘制了行星的路径以及 x_suny_sun 的情节在 x_graphy_graph 的同一情节中标记 = ' o',其中位置对于整个动画是固定的,即在 (-0.8,0)。此外,我还需要将行星的运动显示为一个围绕太阳移动的蓝点,在后面留下一条小轨迹,但不是其运动的完整路径。

这是我的代码,但它似乎不起作用。输出只是作为动画的行星运动。我没有从 x_suny_sun 的图中得到那个蓝点,它应该在 (-0.8, 0)

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

n = int(input("Enter the number of steps\n"))
s = int(n/2)

def f(r, u, t):
    return 1/r**3-1/r**2  # Equation of r

def g(theta, r, t):
    return 1/r**2  # Equation of theta

x_graph = []
y_graph = []

x1 = []
y1 = []

def func(r_0, theta_0, u_0, t_0, h):
    x = -0.98  # initial value of x
    y = 0      # initial value of y
    for i in range(1, n + 1):
        m1 = h * u_0
        k1 = h * f(r_0, u_0, t_0)
        l1 = h * g(theta_0, r_0, t_0)
        m2 = h * (u_0 + 0.5 * k1)
        k2 = h * f(r_0 + 0.5 * m1, u_0 + 0.5 * k1, t_0 + 0.5 * h)
        l2 = h * g(theta_0 + 0.5 * l1, r_0 + 0.5 * k1, t_0 + 0.5 * h)
        m3 = h * (u_0 + 0.5 * k2)
        k3 = h * f(r_0 + 0.5 * m2, u_0 + 0.5 * k2, t_0 + 0.5 * h)
        l3 = h * g(theta_0 + 0.5 * l2, r_0 + 0.5 * k2, t_0 + 0.5 * h)
        m4 = h * (u_0 + k3)
        k4 = h * f(r_0 + m3, u_0 + k3, t_0 + h)
        l4 = h * g(theta_0 + l3, r_0 + k3, t_0 + h)
        r_0 += (m1 + 2 * m2 + 2 * m3 + m4) / 6
        u_0 += (k1 + 2 * k2 + 2 * k3 + k4) / 6
        theta_0 += (l1 + 2 * l2 + 2 * l3 + l4) / 6
        x = r_0 * np.cos(theta_0)
        y = r_0 * np.sin(theta_0)
        t_0 += h
        x_graph.append(x)
        y_graph.append(y)
    return x, y

fig = plt.figure()
p1 = fig.add_subplot(111)

l1, = p1.plot([],[])
l2, = p1.plot([],[],marker= 'o', ls='')

print(func(0.98, -np.pi, 0, 0, 0.001))
x_sun = np.linspace(-0.8, -0.8, len(x_graph))
y_sun = np.linspace(0,0,len(y_graph))
plt.xlim(-1.2,1.5)
plt.ylim(-1.5,1.5)
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.title('Planet\'s orbit')

def polar_animator(i):
    l1.set_data(x_graph[:i], y_graph[:i])
    return l1, 
def animate(i):
    l2.set_data(x_sun[:i], y_sun[:i])
    return l2,
    
ani = animation.FuncAnimation(fig, polar_animator, frames= len(x_graph), interval=1, 
blit=True)
anim = animation.FuncAnimation(fig, animate, interval=1, blit=True)
ani.save('planet.mp4', writer= 'ffmpeg')

目前的输出看起来像这样,但我想在点 (-0.8, 0) 有一个标记(像一个大点)代表太阳。由于 SO 不允许嵌入视频,因此我无法附加视频。我正在附加一个 .png

【问题讨论】:

  • 欢迎来到 SO!您的问题过于宽泛,可能会受到较少的关注。在 SO 中添加以下信息是一个很好的做法:您的预期、当前输出的外观以及您的调试尝试?
  • 您可以随时embed a gif

标签: python numpy matplotlib matplotlib-animation


【解决方案1】:

在我的机器上运行您的代码会得到以下输出:

有些事情看起来不合时宜。

动画被调用了两次(这导致我的电脑闪烁)。因此,只需调用一次动画并更新两个元素(事实上,您只需触摸一次绘制的太阳,这样您仍然可以每次都剪掉不必要的绘制太阳)。

def animate(i):
    l2.set_data(x_sun[:i], y_sun[:i])
    l1.set_data(x_graph[:i], y_graph[:i])
    return l2,l1,
    

调用一次动画函数如下:

anim = animation.FuncAnimation(fig, animate, frames=100, interval=1, blit=True)

【讨论】:

  • 如果sun 不需要更新,将它绘制在ini() 函数中不是更好吗?不应该是frames=n 或类似的东西吗?
  • @vvy。这工作正常。谢谢你。我想有这样的行星运动instagram.com/p/B_sCllahtjZ>
  • @Mr.T 我对动画知之甚少。这是我第一次制作。我不知道ini()函数是什么
  • @Mr.T 我认为您指的是animation.FuncAnimationinit_func arg。我认为太阳可以在l1的初始化中绘制如下l1, = p1.plot(x_val, y_val, marker='o')。 @Arnav 如果答案解决了原始问题,您可以接受。
  • @vvy 如果我采取 l1,正如你所说,在评论中,它不起作用。它只是绘制太阳而不是行星
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多