我需要一些帮助来制作动画情节

发布于 2025-01-29 04:35:43 字数 882 浏览 2 评论 0原文

我有一个数组x_trj具有Shape (50,12),我想为与2-D Plot中某些列的所有行制作一个动画(因此我可以看到每条线的方向如何)。以下是我的代码:

from matplotlib.animation import FuncAnimation

fig,ax = plt.subplots()

# Plot initial line to fill in as we go
line, = ax.plot([], [], lw=2)

def init():
    line.set_data([], [])
    return line,

def animate(i):
    # Plot resulting trajecotry of car
    
    line.set_xdata(x_trj[i,0:9:4])
  

    line.set_ydata(x_trj[i,1:10:4])
   

    return line,


anim = FuncAnimation(fig, animate, init_func = init, frames=x_trj.shape[0], interval=200) # animation doesn't show up?

但是,动画根本不会显示。我得到的只是一个空的数字。我应该如何解决此问题?我正在Google Colab中编写代码。 动画显示为空的(它具有内置播放按钮):

”在此处输入图像描述”

I have an array x_trj that has shape (50,12), and I would like to make an animation for all the rows corresponding to certain columns in a 2-D plot (so I can see what direction each line is going). Below is my code:

from matplotlib.animation import FuncAnimation

fig,ax = plt.subplots()

# Plot initial line to fill in as we go
line, = ax.plot([], [], lw=2)

def init():
    line.set_data([], [])
    return line,

def animate(i):
    # Plot resulting trajecotry of car
    
    line.set_xdata(x_trj[i,0:9:4])
  

    line.set_ydata(x_trj[i,1:10:4])
   

    return line,


anim = FuncAnimation(fig, animate, init_func = init, frames=x_trj.shape[0], interval=200) # animation doesn't show up?

However, the animation does not show up at all. All I get is an empty figure. How should I fix this issue? I am writing my code in Google colab.
The animation shows up but it's empty (it has a built-in play button):

enter image description here

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

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

发布评论

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

评论(1

━╋う一瞬間旳綻放 2025-02-05 04:35:43

我不知道您在x_traj中拥有什么值区域会自动显示在我可以看到的区域中。

我仅在本地计算机上进行了测试。

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

np.random.seed(0)  # `randint()` will always select the same values
x_trj = np.random.randint(0, 10, (52,12))

fig, ax = plt.subplots()
ax.set_xlim(0, 10)  # it would need to get min(), max() values from x_trj
ax.set_ylim(0, 10)  # it would need to get min(), max() values from x_trj

#ax = plt.axes(xlim=(0, 10), ylim=(0, 10))  # needs `ax =`

line, = ax.plot([], [], lw=2)

def init():
    line.set_data([], [])
    return line,

def animate(i):
    print('i:', i)

    x = x_trj[i,0:9:4]
    y = x_trj[i,1:10:4]
    print('x,y:', x, y)

    line.set_xdata(x)
    line.set_ydata(y)

    return line,

anim = FuncAnimation(fig, animate, init_func=init, frames=x_trj.shape[0], interval=200)

plt.show()

最终,您可以在动画期间更改xlim,ylim更改可见区域,但这可能不会如您期望的那样。

def animate(i):
    print('i:', i)

    x = x_trj[i,0:9:4]
    y = x_trj[i,1:10:4]
    print('x,y:', x, y)

    line.set_xdata(x)
    line.set_ydata(y)

    ax.set_xlim(min(x), max(x))  # change visible are
    ax.set_ylim(min(y), max(y))  # change visible are

    return line,

I don't know what values you have in x_traj but I had to set xlim, ylim to see animation because animation doesn't change visible area automatically and it displays line in area which I can see.

I tested it only on local computer.

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

np.random.seed(0)  # `randint()` will always select the same values
x_trj = np.random.randint(0, 10, (52,12))

fig, ax = plt.subplots()
ax.set_xlim(0, 10)  # it would need to get min(), max() values from x_trj
ax.set_ylim(0, 10)  # it would need to get min(), max() values from x_trj

#ax = plt.axes(xlim=(0, 10), ylim=(0, 10))  # needs `ax =`

line, = ax.plot([], [], lw=2)

def init():
    line.set_data([], [])
    return line,

def animate(i):
    print('i:', i)

    x = x_trj[i,0:9:4]
    y = x_trj[i,1:10:4]
    print('x,y:', x, y)

    line.set_xdata(x)
    line.set_ydata(y)

    return line,

anim = FuncAnimation(fig, animate, init_func=init, frames=x_trj.shape[0], interval=200)

plt.show()

Eventually you can change xlim, ylim during animation to change visible area but this may not look as you expect.

def animate(i):
    print('i:', i)

    x = x_trj[i,0:9:4]
    y = x_trj[i,1:10:4]
    print('x,y:', x, y)

    line.set_xdata(x)
    line.set_ydata(y)

    ax.set_xlim(min(x), max(x))  # change visible are
    ax.set_ylim(min(y), max(y))  # change visible are

    return line,
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文