python - 来自带有 matplotlib 的数组的 3D 曲线的线条颜色

标签 python numpy matplotlib 3d mplot3d

我想在 Z 转弯中用不同颜色的点绘制一条 3D 线。 我将 Visual Studio 2013 与 Python 一起使用,我必须读取 .json(XML 样式)文件并将其(X、Y、Z)绘制到 3D 图。 所以我得到了一种颜色的曲线: enter image description here 我想要这样: enter image description here

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

object of type 'numpy.float64' has no len() (says 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' 时出现错误:

object of type 'numpy.float64' has no len() (says 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()

enter image description here

在你的代码循环中必须是这样的:

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))

关于python - 来自带有 matplotlib 的数组的 3D 曲线的线条颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37856209/

相关文章:

python - 使用 numpy 分隔数组以在 python 中使用 matplotlib 进行绘图

python - 如何有效地将一个 Pandas Dataframe 的每一列与另一个 Dataframe 的每一列相乘?

python - Dialogflow - 通过 v2 API 创建带有输入上下文的 Intent

python - 从其他 many2many 字段中填充 many2many 字段

python - 从文件读取后写回同一个文件

python - 获取 Pandas DataFrame 可读数据类型

Python HTML实时绘图

python - 在图表上绘制单点

python - 在 Python3 的 Matplotlib 的极坐标/径向条形图中获取条形图顶部的标签

python - 有效地搜索一对有噪声的有序列表