Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 每次迭代更改3D散点图的颜色_Python_Numpy_Matplotlib_Mplot3d - Fatal编程技术网

Python 每次迭代更改3D散点图的颜色

Python 每次迭代更改3D散点图的颜色,python,numpy,matplotlib,mplot3d,Python,Numpy,Matplotlib,Mplot3d,该图在每次迭代中生成一组点。但在所有迭代结束时,不可能区分不同代的点。那么,是否有可能为每一代点添加不同的颜色?此外,还可以进行n次迭代。添加颜色列表并迭代: e、 g.将这些行添加到代码中 #3d dynamic scatterplot import numpy as np from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Axes3D import time plt.ion() fig = plt.f

该图在每次迭代中生成一组点。但在所有迭代结束时,不可能区分不同代的点。那么,是否有可能为每一代点添加不同的颜色?此外,还可以进行n次迭代。

添加颜色列表并迭代:

e、 g.将这些行添加到代码中

#3d dynamic scatterplot
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import time

plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
s=0
a=1
b=2
for i in range(0, 10):   
    s=a+b
    ax.set_xlabel('X axis')
    ax.set_ylabel('Y axis')
    ax.set_zlabel('Z axis')
    x = np.random.rand(5, 3) 
    y = np.random.rand(5, 3)
    z = np.random.rand(5, 3)
    #ax.cla()    
    ax.scatter(x[:, 0], y[:, 1], z[:, 2])
    plt.draw()
    time.sleep(1)   #make changes more apparent/easy to see
    a=a+1
    b=b+2
    if s>10:
         break;
您的代码是:

colors = ['#8ffe09','r','#0033ff','#003311','#993333','#21c36f','#c46210','#ed3cca','#ffbf00','g','#000000'] # a list of colours


ax.scatter(x[:, 0], y[:, 1], z[:, 2],color=colors[a-1]) # use the color kwarg
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import time
colors = ['#8ffe09','r','#0033ff','#003311','#993333','#21c36f','#c46210','#ed3cca','#ffbf00','g','#000000']
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
s=0
a=1
b=2
for i in range(0, 10):   
    s=a+b
    ax.set_xlabel('X axis')
    ax.set_ylabel('Y axis')
    ax.set_zlabel('Z axis')
    x = np.random.rand(5, 3) 
    y = np.random.rand(5, 3)
    z = np.random.rand(5, 3)
    #ax.cla()    
    ax.scatter(x[:, 0], y[:, 1], z[:, 2],color=colors[a-1])
    plt.draw()
    time.sleep(1)   #make changes more apparent/easy to see
    a=a+1
    b=b+2
    if s>10:
         break;