2017-11-25 132 views
-2

我想製作一個動畫散點圖。這是一個MWE。需要旋轉動畫散點圖才能在Mac上更新?

from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 
from matplotlib import cm 
import matplotlib.animation 
import numpy as np 
from numpy import cos, pi 

fig = plt.figure() 
ax = fig.gca(projection='3d') 

X = np.arange(-5, 5, 0.25) 
Y = np.arange(-5, 5, 0.25) 
X, Y = np.meshgrid(X, Y) 
Z = 20 + X**2 + Y**2 - 10*cos(2*pi*X) - 10*cos(2*pi*Y) 
# surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, 
#      linewidth=0, antialiased=False) #This should work uncommented 

X = X.flatten() 
Y = Y.flatten() 
Z = Z.flatten() 

points = ax.scatter(X,Y,Z) 
def animate(i): 
    points._offsets3d = (X,Y,Z/i) 
    return points 

ani = matplotlib.animation.FuncAnimation(fig,animate,range(1,10),interval=1000,blit=False,repeat=False) 
plt.show() 

據我所知,我使用默認mac後端的macbook air。如果您單擊並旋轉圖形,則可以看到散點圖更新,但不會自行顯示更新。

回答

-1

對於任何人誰說到這個,我改變了後端「TKAGG」使用在MWE頂部以下內容:

import matplotlib 
matplotlib.use("TKAGG")