首页 > 解决方案 > 如何使用 matplotlib 和赛璐珞放置动态图例

问题描述

我正在尝试模拟粒子的运动,并在粒子移动时显示时间戳。如何将时间戳/图例放置在固定位置(比如右上角)?

这是我迄今为止所拥有的:

import numpy as np
from matplotlib import pyplot as plt
from IPython.display import Image, Video
from celluloid import Camera


m1, m2, v0     = 1, 1, 10   # masses, vel ini
x1, y1, x2, y2 = 1, 1, 100, 1 # coordinates

fig =  plt.figure()
cam =  Camera(fig)

def xf(t):
    return x1 + v0*t


def xf2(t):           # After the collision
    v = m1*v0/(m1+m2) # Conservation of momentum
    return x2 + v*t


for t in range(20):
    if xf(t) < x2:
        plt.plot(xf(t), y1, 'ro', label = str(t))
        plt.plot(x2, y2, 'bo')
        plt.legend()
        cam.snap()

anim = cam.animate()
anim.save('momentum.gif', writer = 'imagemagick')
Image('momentum.gif')

标签: animationlegendcelluloid

解决方案


推荐阅读