簡體   English   中英

如何在matplotlib中為3d plot_surface設置動畫

[英]How to animate 3d plot_surface in matplotlib

我已經從文件創建了一個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()

這可能不是最佳方式,但我發現文檔/示例也不夠。

我使用的是以下解決方案:使用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,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM