python - 我想在星球动画中留下足迹
问题描述
我一直在研究一个涉及行星围绕恒星的动画的 2 体问题。我希望我的星球在移动这样的东西时留下一点痕迹。除了这一件事,我的代码完美地绘制了所有内容。我无法得到这个星球的踪迹。
fig, ax = plt.subplots()
print(func(0.98, -np.pi, 0, 0, 0.001))
ax.set(xlim = (-1.2,1.5), ylim = (-1.5,1.5), xlabel = 'x axis', ylabel='y axis', title = 'Planet\'s orbit')
def polar_animator(i):
trail = 40
l1.set_data(x_graph[i-1:i], y_graph[i-1:i])
return l1,
l1, = ax.plot([],[], 'o-')
l2, = ax.plot([-0.8],[0],marker= 'o')
ani = animation.FuncAnimation(fig, polar_animator, frames= len(x_graph), interval=5, blit=True)
ani.save('planet.mp4', writer= 'ffmpeg')
我得到的输出只是一个围绕太阳移动的球。
解决方案
我认为你很接近。我所做的修改是:
- 保留
trail
要在图表上显示的最后一个点(您只显示最后 2 个点) markevery=[-1]
在调用中使用plot()
以仅在行尾显示标记。
完整代码:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
x_graph = np.linspace(0, 1, 100)
y_graph = x_graph
trail = 40
fig, ax = plt.subplots()
ax.set(xlim=(-1.2, 1.5), ylim=(-1.5, 1.5), xlabel='x axis', ylabel='y axis', title='Planet\'s orbit')
def polar_animator(i, trail=40):
l1.set_data(x_graph[i - trail:i], y_graph[i - trail:i])
return l1,
l1, = ax.plot([], [], 'o-', markevery=[-1])
l2, = ax.plot([-0.8], [0], marker='o')
ani = animation.FuncAnimation(fig, polar_animator, frames=len(x_graph), fargs=(trail,), interval=5, blit=True)
推荐阅读
- c# - 继承类是否必须使用与其基类相同的参数才能进行依赖注入?
- javascript - 未在另一个函数中定义的函数
- javascript - Select2 在数据加载后立即关闭
- conda - conda 与 linux 环境模块的用例
- xml - JAVA JAX-WS 使用自定义 SOAPHandler 将 XML 转换为 SOAP
- vb.net - 错误,因为索引超出了数组的范围?
- flutter - 如何在 Flutter 中更改 ElevatedButton 的背景颜色?
- python - python动态嵌套字典到csv
- swift - 在 Vapor 命令中使用 Fluent
- prometheus - 随着时间的推移获得平均增长