2017-09-16 4 views
0

Je suis en train de créer un terrain 3D et le code que j'ai écrit est:tracé 3D avec python numpy

import matplotlib as mpl 
from mpl_toolkits.mplot3d import Axes3D 
import numpy as np 

fig = plt.figure() 
ax = fig.gca(projection='3d') 
a=[1,5,10] 
x=list(range(1,10)) 
y=list(range(1,10)) 
z=list(range(1,10)) 
for i in np.arange(1,9,1): 
    r1=a[2]*y[i]-a[1]*z[i] 
    r2=a[0]*z[i]-a[2]*x[i] 
    r3=a[1]*x[i]-a[0]*y[i] 
    ax.plot(r1,r2,r3) 

Pour cela, j'obtiens l'erreur:

TypeError         Traceback (most recent call 
last) 
<ipython-input-95-dd97076f2b6e> in <module>() 
    14  r2=a[0]*z[i]-a[2]*x[i] 
    15  r3=a[1]*x[i]-a[0]*y[i] 
---> 16  ax.plot(r1,r2,r3) 

    1537   # Match length 
    1538   if not cbook.iterable(zs): 
-> 1539    zs = np.ones(len(xs)) * zs 
    1540 
    1541   lines = Axes.plot(self, xs, ys, *args[argsi:], 
**kwargs) 

TypeError: object of type 'int' has no len() 

Il ressemble à quelque chose est mal avec la commande plot, mais je ne peux pas le localiser. Toute aide est appréciée.

Répondre

0

Il semble y avoir quelques erreurs dans le code:

Dans mon cas, j'ai réussi à courir avec:

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

fig = plt.figure() 
ax = fig.gca(projection='3d') 
a=[1,5,10] 
x=np.array(range(1,10)) 
y=np.array(range(1,10)) 
z=np.array(range(1,10)) 


r1=a[2]*y-a[1]*z 
r2=a[0]*z-a[2]*x 
r3=a[1]*x-a[0]*y 

ax.plot(r1,r2,r3) 
plt.show() 

J'ai changé l'importation afin que vous importez réellement pyplot comme plt (que vous utilisez dans votre code d'origine dans plt.figure())

Ensuite, au lieu d'utiliser des listes pour x, y et z je tableaux cahoteuses qui vous permettent d'ajouter et de les multiplier ensemble comme dans:

Enfin, j'ai ajouté plt.show() qui est nécessaire dans mon IDE pour afficher réellement le tracé, mais il est possible que vous n'en ayez pas besoin.

0

Je l'ai à travailler avec le code suivant:

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

def crossprod(val): 
    return [(a[2]-a[1])*val,(a[0]-a[2])*val,(a[1]-a[0])*val] 

mpl.rcParams['legend.fontsize'] = 10 
fig = plt.figure() 
ax = fig.gca(projection='3d') 
a=[1,0,0] 
array=[] 
for i in np.arange(-50,50): 
    cr=crossprod(i) 
    array.append(cr) 
Array=np.vstack(array) 
x=Array[:,[0]].ravel() 
y=Array[:,[1]].ravel() 
z=Array[:,[2]].ravel() 
ax.plot(x,y,z, label='a=%s' %a) 
ax.legend() 
plt.show()