需要旋转动画散点图以在Mac上更新?

时间:2017-11-25 19:25:55

标签: python animation matplotlib scatter-plot

我正在尝试制作动画散点图。这是一个MWE。

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 个答案:

答案 0 :(得分:-1)

对于任何参与此事的人,我都将后端更改为" TKAGG"使用MWE顶部的以下内容:

import matplotlib
matplotlib.use("TKAGG")