移动球体动画

发布于 2025-01-26 20:38:19 字数 866 浏览 2 评论 0原文

我想在matplotlib中创建一个移动球体的动画。由于某种原因,它不起作用:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib import cm
from matplotlib import animation
import pandas as pd


fig = plt.figure(facecolor='black')
ax = plt.axes(projection = "3d")

u = np.linspace(0, 2*np.pi, 100)
v = np.linspace(0, np.pi, 100)
r = 4

ax.set_xlim(0, 60)
ax.set_ylim(0, 60)
ax.set_zlim(0, 60)

x0 = r * np.outer(np.cos(u), np.sin(v)) + 10
y0 = r * np.outer(np.sin(u), np.sin(v)) + 10
z0 = r * np.outer(np.ones(np.size(u)), np.cos(v)) + 50

def init():
    ax.plot_surface(x0,y0,z0)
    return fig,

def animate(i):
    ax.plot_surface(x0 + 1, y0 + 1, z0 + 1)
    return fig,

ani = animation. FuncAnimation(fig, animate, init_func = init, frames = 90, interval = 300)


plt.show()

在这里,我试图在每次新迭代中以(1,1,1)移动球体,但是它没有这样做。

I want to create an animation of a moving sphere in matplotlib. For some reason it isnt working:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib import cm
from matplotlib import animation
import pandas as pd


fig = plt.figure(facecolor='black')
ax = plt.axes(projection = "3d")

u = np.linspace(0, 2*np.pi, 100)
v = np.linspace(0, np.pi, 100)
r = 4

ax.set_xlim(0, 60)
ax.set_ylim(0, 60)
ax.set_zlim(0, 60)

x0 = r * np.outer(np.cos(u), np.sin(v)) + 10
y0 = r * np.outer(np.sin(u), np.sin(v)) + 10
z0 = r * np.outer(np.ones(np.size(u)), np.cos(v)) + 50

def init():
    ax.plot_surface(x0,y0,z0)
    return fig,

def animate(i):
    ax.plot_surface(x0 + 1, y0 + 1, z0 + 1)
    return fig,

ani = animation. FuncAnimation(fig, animate, init_func = init, frames = 90, interval = 300)


plt.show()

Here, I have attempted to move the sphere by (1,1,1) in each new iteration, but it fails to do so.

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

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

发布评论

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

评论(1

撩发小公举 2025-02-02 20:38:19

您的方法有几个错误:

  1. 在您的Animate功能中,您在每个迭代中添加一个领域。不幸的是,poly3dcollection对象(ax.plot_surface创建后创建后创建的),因此无法修改它们,因此,为了使我们需要删除上一个迭代的表面和添加一个新的。
  2. 在您的动画中,球没有移动,因为在每次迭代中,您都在与上一个位置相同的位置添加一个新球体。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib import cm
from matplotlib import animation
import pandas as pd


fig = plt.figure(facecolor='black')
ax = plt.axes(projection = "3d")

u = np.linspace(0, 2*np.pi, 100)
v = np.linspace(0, np.pi, 100)
r = 4

ax.set_xlim(0, 60)
ax.set_ylim(0, 60)
ax.set_zlim(0, 60)

x0 = r * np.outer(np.cos(u), np.sin(v)) + 10
y0 = r * np.outer(np.sin(u), np.sin(v)) + 10
z0 = r * np.outer(np.ones(np.size(u)), np.cos(v)) + 50

surface_color = "tab:blue"

def init():
    ax.plot_surface(x0, y0, z0, color=surface_color)
    return fig,

def animate(i):
    # remove previous collections
    ax.collections.clear()
    # add the new sphere
    ax.plot_surface(x0 + i, y0 + i, z0 + i, color=surface_color)
    return fig,

ani = animation. FuncAnimation(fig, animate, init_func = init, frames = 90, interval = 300)


plt.show()

There are a couple of mistakes with your approach:

  1. In your animate function you are adding a sphere at each iteration. Unfortunately, Poly3DCollection objects (created by ax.plot_surface) cannot be modified after they have been created, hence to animate a surface we need to remove the surface of the previous iteration and add a new one.
  2. In your animation the sphere didn't move because at each iteration you were adding a new sphere at the same location as the previous one.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib import cm
from matplotlib import animation
import pandas as pd


fig = plt.figure(facecolor='black')
ax = plt.axes(projection = "3d")

u = np.linspace(0, 2*np.pi, 100)
v = np.linspace(0, np.pi, 100)
r = 4

ax.set_xlim(0, 60)
ax.set_ylim(0, 60)
ax.set_zlim(0, 60)

x0 = r * np.outer(np.cos(u), np.sin(v)) + 10
y0 = r * np.outer(np.sin(u), np.sin(v)) + 10
z0 = r * np.outer(np.ones(np.size(u)), np.cos(v)) + 50

surface_color = "tab:blue"

def init():
    ax.plot_surface(x0, y0, z0, color=surface_color)
    return fig,

def animate(i):
    # remove previous collections
    ax.collections.clear()
    # add the new sphere
    ax.plot_surface(x0 + i, y0 + i, z0 + i, color=surface_color)
    return fig,

ani = animation. FuncAnimation(fig, animate, init_func = init, frames = 90, interval = 300)


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