Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line colour of 3D curve from an array with matplotlib

I want to draw a 3D line of points with different color in the Z turn. I use the Visual Studio 2013 with Python and I have to read an .json (XML-Style) file and draw it (X,Y,Z) to a 3D Diagram. So I got a curve with one color: enter image description here I want to have it like this: enter image description here

I have a 3 dimension numpy array, but when I write this code down, like in the answer of the link, so I got an error:

object of type 'numpy.float64' has no len() (says VS2013)

my Code in short form:

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

When I take off the value 'color' so I got my blue curve, with the value 'color' I got error:

object of type 'numpy.float64' has no len() (says VS2013).

so I read in the API of matplotlib, but I can't find a solution.

like image 330
P_Gabriel Avatar asked Nov 08 '22 13:11

P_Gabriel


1 Answers

Look at the example and comments in it:

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

In your code cycle have to be like:

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))
like image 71
Serenity Avatar answered Nov 14 '22 21:11

Serenity