3D中的散点图是一个mpl_toolkits.mplot3d.art3d.Path3DCollection
对象.这提供了一个_offsets3d
托管元组的属性,(x,y,z)
可用于更新散点的坐标.因此,不在动画的每次迭代中创建整个绘图可能是有益的,而是仅更新其点.
以下是如何执行此操作的工作示例.
import numpy as np from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Axes3D import matplotlib.animation import pandas as pd a = np.random.rand(2000, 3)*10 t = np.array([np.ones(100)*i for i in range(20)]).flatten() df = pd.DataFrame({"time": t ,"x" : a[:,0], "y" : a[:,1], "z" : a[:,2]}) def update_graph(num): data=df[df['time']==num] graph._offsets3d = (data.x, data.y, data.z) title.set_text('3D Test, time={}'.format(num)) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') title = ax.set_title('3D Test') data=df[df['time']==0] graph = ax.scatter(data.x, data.y, data.z) ani = matplotlib.animation.FuncAnimation(fig, update_graph, 19, interval=40, blit=False) plt.show()
该解决方案不允许blitting.但是,根据使用情况,可能根本不需要使用散点图; 使用法线plot
可能同样可能,允许blitting - 如下例所示.
import numpy as np from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Axes3D import matplotlib.animation import pandas as pd a = np.random.rand(2000, 3)*10 t = np.array([np.ones(100)*i for i in range(20)]).flatten() df = pd.DataFrame({"time": t ,"x" : a[:,0], "y" : a[:,1], "z" : a[:,2]}) def update_graph(num): data=df[df['time']==num] graph.set_data (data.x, data.y) graph.set_3d_properties(data.z) title.set_text('3D Test, time={}'.format(num)) return title, graph, fig = plt.figure() ax = fig.add_subplot(111, projection='3d') title = ax.set_title('3D Test') data=df[df['time']==0] graph, = ax.plot(data.x, data.y, data.z, line, marker="o") ani = matplotlib.animation.FuncAnimation(fig, update_graph, 19, interval=40, blit=True) plt.show()