2017-08-18 287 views
1

我有散射动画这个工作代码2D:散点图Matplotlib 2D> 3D

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
def _update_plot(i, fig, scat): 
    scat.set_offsets(([0, i], [50, i], [100, i])) 
    return scat, 
fig = plt.figure() 
x = [0, 50, 100] 
y = [0, 0, 0] 
ax = fig.add_subplot(111) 
ax.set_xlim([-50, 200]) 
ax.set_ylim([-50, 200]) 
scat = plt.scatter(x, y, c=x) 
scat.set_alpha(0.8) 
anim = animation.FuncAnimation(fig, _update_plot, fargs=(fig, scat), frames=100, interval=100) 
plt.show() 

我试图把它转换成3D与这一点,但它不会工作..

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
import numpy as np 
from mpl_toolkits.mplot3d import Axes3D 


def _update_plot(i, fig, scat): 
    scat._offsets3d([0, 0, 0], [50, 0, 0], [100, 0, 0]) 

    return scat 

fig = plt.figure() 

x = [0, 50, 100] 
y = [0, 0, 0] 
z = [0, 0, 0] 

ax = fig.add_subplot(111, projection='3d') 

scat = ax.scatter(x, y, z) 

anim = animation.FuncAnimation(fig, _update_plot, fargs=(fig, scat), frames=100, interval=100) 

plt.show() 

灿有人请给我一个关于如何解决这个问题的建议? 谢谢

回答

1

_offsets3d是一个属性,而不是一个方法。取而代之的

scat._offsets3d([0, 0, 0], [50, 0, 0], [100, 0, 0]) 

你需要(x,y,z)值的元组分配给它:

scat._offsets3d = ([0, 0, 0], [50, 0, 0], [100, 0, 0]) 

当然,这总是会产生所有100帧相同的情节。所以为了看动画像

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
import numpy as np 
from mpl_toolkits.mplot3d import Axes3D 


def _update_plot(i, fig, scat): 
    scat._offsets3d = ([0, i, i], [50, i, 0], [100, 0, i]) 
    return scat 

fig = plt.figure() 

x = [0, 50, 100] 
y = [0, 0, 0] 
z = [0, 0, 0] 

ax = fig.add_subplot(111, projection='3d') 

scat = ax.scatter(x, y, z) 

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

anim = animation.FuncAnimation(fig, _update_plot, fargs=(fig, scat), frames=100, interval=100) 

plt.show()