【问题标题】:Function animation with plot_surface not drawing , just giving first picture带有 plot_surface 的函数动画不绘图,只给出第一张图片
【发布时间】:2021-10-18 19:00:12
【问题描述】:

我用plot_surface写了下面的函数动画代码,它不是画的,只是给出第一张图片

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

x = np.outer(np.linspace(-2, 2, 50), np.ones(50))
#print(x)
y = x.copy().T # transpose
fig = plt.figure()
ax = plt.axes(projection='3d')

def animation_frame(i):
  z = np.cos(x ** 2 + y ** 2) + np.cos(x ** (2*i) + y ** (2*i)) 
  # print (z)
  
  ax.plot_surface(x, y, z,cmap='viridis', edgecolor='none')
  # return ax,

animation = FuncAnimation(fig, func=animation_frame, frames=np.arange(0, 10, 1), interval=1000,  blit=False)
#plt.show()
animation

【问题讨论】:

  • 希望答案是有帮助的。彻底回答问题很费时间。如果您的问题已解决,请接受解决方案 位于答案左上角的 ▲/▼ 箭头下方。如果出现更好的解决方案,则可以接受新的解决方案。如果您的声望超过 15,您还可以使用 ▲/▼ 箭头对答案的有用性进行投票。 如果解决方案无法回答问题,请发表评论。 What should I do when someone answers my question?。谢谢。
  • 仅供参考:彻底回答问题非常耗时。如果您的问题已解决,请通过接受最适合您的需求的解决方案表示感谢。 位于答案左上角的 / 箭头下方。如果出现更好的解决方案,则可以接受新的解决方案。如果您的声望超过 15,您也可以使用 / 箭头对答案的有用性进行投票。 如果解决方案不能回答问题,请发表评论What should I do when someone answers my question?。谢谢

标签: python matplotlib animation plot data-visualization


【解决方案1】:

您应该在最后调用plt.show() 方法。此外,您应该删除以ax.cla() 开头的animation_frame 之前的绘图。

完整代码

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


x = np.outer(np.linspace(-2, 2, 50), np.ones(50))
y = x.copy().T
fig = plt.figure()
ax = plt.axes(projection = '3d')


def animation_frame(i):
    ax.cla()
    z = np.cos(x**2 + y**2) + np.cos(x**(2*i) + y**(2*i))
    ax.plot_surface(x, y, z, cmap = 'viridis', edgecolor = 'none')


animation = FuncAnimation(fig, func = animation_frame, frames = np.arange(0, 10, 1), interval = 250, blit = False)

plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多