【问题标题】:How to animate 3d plot_surface in matplotlib如何在 matplotlib 中为 3d plot_surface 设置动画
【发布时间】:2013-06-22 09:39:45
【问题描述】:

我已经从一个文件创建了一个 3D 绘图表面,并且我正在尝试为绘图设置动画。 我已经阅读了 matplotlib 网页中的示例和 SO 中的其他示例,并注意到我需要创建一个更新函数来遍历文件中的值,然后创建一个 matplotlib.animation 对象,但我不明白该怎么做它。

如果有人能向我解释更新函数的语法以及如何在matplotlib.animation 对象中使用它,我将不胜感激。

我的数据是一个包含 498 行的多维数组,每行我都有一个包含 64x128 值的数组。数据按以下方式组织:

数据是来自测力台的时间序列,500 行中的每一行都是一个帧,这意味着该试验持续 10 秒。对于每一帧,我都有另一个 64x128 值的数组。

这是我到目前为止的代码:

from mpl_toolkits.mplot3d import *
import matplotlib.pyplot as plt
import numpy as np
from random import random, seed
from matplotlib import cm
from mpl_toolkits.mplot3d import axes3d
import matplotlib.animation as animation

source_path = "c:\\Projecto\\"
destination_path = "c:\\Projecto\\EntirePlate\\"
#fid = np.loadtxt(source_path + "rolloff_xls.txt",dtype=str)

fid_MP = open(source_path + "101mp - Entire plate roll off.xls","Ur")
lines_MP = fid_MP.readlines()
fid_MP.close()

values_MP = []

for i in lines_MP:
      if i[0].isdigit():
          values_MP.append(i)

values = np.loadtxt(values_MP,dtype=float)

new_values_MP =[]

for i in range(0,(len(values_MP)/64)):
    for j in range(0,64):
        new_values_MP.append([[i],[j],values[j]])

new_values_MP = np.asarray(new_values_MP)

fig = plt.figure()
ax = fig.gca(projection='3d')               # to work in 3d
plt.hold(True)

x_surf = np.arange(0,128)                # generate a mesh
y_surf = np.arange(0,64)
x_surf, y_surf = np.meshgrid(x_surf, y_surf)
z_surf = []

for i in range(0,64):
     # print(new_values[i])
     z_surf.append(np.asarray(new_values_MP[i][2])) # ex. function, which depends on x and y

z_surf = np.asarray(z_surf).reshape([64,128])

ax.plot_surface(x_surf, y_surf, z_surf, rstride=2, cstride=2 ,cmap=cm.jet)    # plot a 3d surface plot

ax.set_xlabel('Medio Lateral - Axis')
ax.set_ylabel('Anterior Posterior - Axis')
ax.set_zlabel('Pressure (P)')

def update(x_values, y_values, z_values):
     for i in range(0,len(values_MP)/64):
         x_surf = x_values
         y_surf = y_values
         z_surf.set_data(new_values_MP[i,2])
     return z_surf

ani = animation.FuncAnimation(fig, update, frames=xrange(len(values_MP)/64),
                               interval=50, blit=False)
plt.show()
 

【问题讨论】:

    标签: python matplotlib jquery-animate


    【解决方案1】:

    这可能不是最佳方式,但我发现文档/示例也不够。

    我采用的是以下解决方案:使用 animation.FuncAnimation 调用函数。在该函数中清除并重绘,如下所示:

    from __future__ import division
    from matplotlib import cm
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    import mpl_toolkits.mplot3d.axes3d as p3
    import matplotlib.animation as animation
    import numpy as np
    
    plot_args = {'rstride': 1, 'cstride': 1, 'cmap':
                 cm.bwr, 'linewidth': 0.01, 'antialiased': True, 'color': 'w',
                 'shade': True}
    
    soln = np.zeros((size, size))
    midpoint = size // 2
    soln[midpoint, midpoint] = 1
    
    #first frame
    X = range(size)
    Y = range(size)
    X, Y = np.meshgrid(X, Y)
    plot = ax.plot_surface(X, Y, soln, **plot_args)
    pam_ani = animation.FuncAnimation(fig, data_gen, fargs=(soln, plot),
                                  interval=30, blit=False)
    
    def data_gen(framenumber, soln, plot):
        #change soln variable for the next frame
        ...
        ax.clear()
        plot = ax.plot_surface(X, Y, soln, **plot_args)
        return plot,
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    • 2016-02-10
    相关资源
    最近更新 更多