Python 使用matplotlib从阵列获得的三维曲线的线颜色

Python 使用matplotlib从阵列获得的三维曲线的线颜色,python,numpy,matplotlib,3d,mplot3d,Python,Numpy,Matplotlib,3d,Mplot3d,我想在Z方向上画一条不同颜色的点的3D线。 我将VisualStudio2013与Python一起使用,我必须读取一个.json(XML样式)文件,并将其(X,Y,Z)绘制成一个3D图。 所以我得到了一条只有一种颜色的曲线: 我想让它像这样: 我有一个三维numpy数组,但当我写下这段代码时,就像在链接的答案中一样,我得到了一个错误: “numpy.float64”类型的对象没有len()(表示VS2013) 我的简短代码: matrix_array = [[0,0,0]] ... ->

我想在Z方向上画一条不同颜色的点的3D线。 我将VisualStudio2013与Python一起使用,我必须读取一个.json(XML样式)文件,并将其(X,Y,Z)绘制成一个3D图。 所以我得到了一条只有一种颜色的曲线: 我想让它像这样:

我有一个三维numpy数组,但当我写下这段代码时,就像在链接的答案中一样,我得到了一个错误:

“numpy.float64”类型的对象没有len()(表示VS2013)

我的简短代码:

matrix_array = [[0,0,0]]
...
-> write data in array
....
matrix_array = np.array(matrix_array)

fig = pyplot.figure()
ax3d = fig.add_subplot(111, projection='3d')
N = len(matrix_array[:,2]) # Z (N now about 14689)
for i in xrange(N-2):
    ax3d.plot(matrix_array[i+2,0],matrix_array[i+2,1],matrix_array[i+2,2], color=pyplot.cm.jet(255*i/N))
当我取下值“color”时,我得到了蓝色曲线,使用值“color”我得到了错误:

“numpy.float64”类型的对象没有len()(表示VS2013)


因此,我阅读了matplotlib的API,但找不到解决方案。

请查看示例和其中的注释:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

N = 100 # number of points
x = np.arange(N, dtype=float) # x,y,z = 1d arrays
y = x * x
z = np.random.rand(N)

fig = plt.figure()
ax = fig.gca(projection='3d')

# you have to plot segments, its means your arguments
# have to be a slice of arrays like here: from x(i-1) to x(i) => x[i-1:i+1]
# to get color from colormap use index: 0 <= i <= 1
for i in xrange(1,N):
    ax.plot(x[i-1:i+1], y[i-1:i+1], z[i-1:i+1], c = plt.cm.jet(1. * i / N))

plt.show()

aaarrrggg。。。那太容易了。非常感谢你的回答。所以现在我的代码看起来像:
对于xrange(1,N)中的i:a=(1.*i/N)ax3d.plot(矩阵数组[i-1:i+1,0],矩阵数组[i-1:i+1,1],矩阵数组[i-1:i+1,2],color=pyplot.cm.jet(a))
但是现在我的代码需要10000点1分钟来构建图形。你现在知道更快的方法了吗?
for i in xrange(1,N):
    ax3d.plot(matrix_array[i-1:i+1,0],matrix_array[i-1:i+1,1],matrix_array[i-1:i+1,2], color=pyplot.cm.jet(1.*i/N))