我正在尝试保存 3d 散点图动画,其中点一次出现一个。我使动画正常工作,但是当我设置点的面颜色时,它们没有生效并且所有点都显示为蓝色。当我在静态图像上使用相同的颜色数组时,颜色效果很好。动画代码:import pandas as pdimport numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Dimport matplotlib.animationimport randomimport seaborn as snsimport pandas as pdimport jsonimport osfrom matplotlib.animation import FuncAnimationimport mpl_toolkits.mplot3d.axes3d as p3from matplotlib import rcfrom IPython.display import HTMLfrom itertools import productx=[]y=[]for i in range(-80, 80, 10): x.append(i) y.append(i)combs = list(product(x,y))def obj(x, y): global HISTORY e = 2.718 res = 7*x*y/(e**(0.001*x**2 + 0.001*y**2)) return resz = [obj(x,y) for x, y in combs]x = [obj[0] for obj in combs]y = [obj[1] for obj in combs]data = [[x[i],y[i],z[i]] for i in range(len(x))]cmap = sns.cubehelix_palette(as_cmap=True)m = max(z) # Get the worst score so we can use it as the darkest area of the plot.face_colors = np.array([cmap(i/m) for i in z]) # Map all of the values with cmap colors. df = pd.DataFrame(data, columns=["x","y","z"])fig = plt.figure()ax = fig.add_subplot(111,projection='3d')sc = ax.scatter([],[],[], alpha=0.5)def update(i): sc._offsets3d = (df.x.values[:i], df.y.values[:i], df.z.values[:i]) sc._facecolors3d = face_colors[:i] sc._facecolors2d=sc._facecolors3dax.set_xlabel('X')ax.set_ylabel('Y')ax.set_zlabel('Z')ax.set_xlim(min(x),max(x))ax.set_ylim(min(y),max(y))ax.set_zlim(min(z),max(z))ani = matplotlib.animation.FuncAnimation(fig, update, frames=len(df), interval=70)HTML(ani.to_html5_video())当我不使用动画而只调用 plt.scatter 时,如下所示:sc = ax.scatter(df.x.values,df.y.values,df.z.values, facecolors=face_colors)我的图像效果很好:如何在动画中保留这些颜色?
1 回答
慕婉清6462132
TA贡献1804条经验 获得超2个赞
可能只是一个错字。_facecolor3d
而不是_facecolors3d
尝试这个:
def update(i): sc._offsets3d = (df.x.values[:i], df.y.values[:i], df.z.values[:i]) sc._facecolor3d = face_colors[:i] sc._edgecolor3d = face_colors[:i]
添加回答
举报
0/150
提交
取消