我正在嘗試保存 3d 散點(diǎn)圖動(dòng)畫,其中點(diǎn)一次出現(xiàn)一個(gè)。我使動(dòng)畫正常工作,但是當(dāng)我設(shè)置點(diǎn)的面顏色時(shí),它們沒有生效并且所有點(diǎn)都顯示為藍(lán)色。當(dāng)我在靜態(tài)圖像上使用相同的顏色數(shù)組時(shí),顏色效果很好。動(dòng)畫代碼: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())當(dāng)我不使用動(dòng)畫而只調(diào)用 plt.scatter 時(shí),如下所示:sc = ax.scatter(df.x.values,df.y.values,df.z.values, facecolors=face_colors)我的圖像效果很好:如何在動(dòng)畫中保留這些顏色?
1 回答

慕婉清6462132
TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個(gè)贊
可能只是一個(gè)錯(cuò)字。_facecolor3d
而不是_facecolors3d
嘗試這個(gè):
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]
添加回答
舉報(bào)
0/150
提交
取消