Matplotlib 3d scatter _facecolors3d 不起作用

我正在尝试保存 3d 散点图动画,其中点一次出现一个。我使动画正常工作,但是当我设置点的面颜色时,它们没有生效并且所有点都显示为蓝色。当我在静态图像上使用相同的颜色数组时,颜色效果很好。


动画代码:


import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

import matplotlib.animation

import random

import seaborn as sns

import pandas as pd

import json

import os

from matplotlib.animation import FuncAnimation

import mpl_toolkits.mplot3d.axes3d as p3

from matplotlib import rc

from IPython.display import HTML

from itertools import product


x=[]

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 res

z = [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._facecolors3d


ax.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())

https://img1.sycdn.imooc.com/6526b4e5000103b406540366.jpg

当我不使用动画而只调用 plt.scatter 时,如下所示:


sc = ax.scatter(df.x.values,df.y.values,df.z.values, facecolors=face_colors)

我的图像效果很好:

https://img1.sycdn.imooc.com/6526b4f00001f46206410435.jpg

如何在动画中保留这些颜色?



大话西游666
浏览 97回答 1
1回答

慕婉清6462132

可能只是一个错字。_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]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python