簡體   English   中英

使用matplotlib從數組中獲取3D曲線的線條顏色

[英]Line colour of 3D curve from an array with matplotlib

我想在Z轉中繪制一條具有不同顏色的3D線。 我使用Visual Studio 2013和Python,我必須讀取.json(XML樣式)文件並將其繪制(X,Y,Z)到3D圖表。 所以我得到了一種顏色的曲線: 在此輸入圖像描述 我希望這樣: 在此輸入圖像描述

我有一個3維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()

在此輸入圖像描述

在你的代碼周期必須像:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM