2011-01-07 22 views
47

ich war nicht erfolgreich Browsing Web nach einer Lösung für die folgende einfache Frage:Plotten 3D-Polygone in Python-matplotlib


Wie 3D-Polygon (sagt sie ein gefülltes Rechteck oder Dreieck) unter Verwendung von Scheitelwerten ziehen? Ich habe viele Ideen ausprobiert, aber alle gescheitert, siehe:

from mpl_toolkits.mplot3d import Axes3D 
from matplotlib.collections import PolyCollection 
import matplotlib.pyplot as plt 
fig = plt.figure() 
ax = Axes3D(fig) 
x = [0,1,1,0] 
y = [0,0,1,1] 
z = [0,1,0,1] 
verts = [zip(x, y,z)] 
ax.add_collection3d(PolyCollection(verts),zs=z) 
plt.show() 

ich eine Idee/Kommentar im Voraus zu schätzen wissen.

Updates basierend auf der akzeptierte Antwort:

import mpl_toolkits.mplot3d as a3 
import matplotlib.colors as colors 
import pylab as pl 
import scipy as sp 

ax = a3.Axes3D(pl.figure()) 
for i in range(10000): 
    vtx = sp.rand(3,3) 
    tri = a3.art3d.Poly3DCollection([vtx]) 
    tri.set_color(colors.rgb2hex(sp.rand(3))) 
    tri.set_edgecolor('k') 
    ax.add_collection3d(tri) 
pl.show() 

Hier ist das Ergebnis: enter image description here

+1

Wie kann diese Lösung für Python 3.5 aktualisiert werden? Diese Version gibt mir den Fehler, dass 'TypeError: Objekt vom Typ 'zip' kein len()' – jlt199

+1

@ jlt199 hat Verwenden Sie einfach '[list (zip (...))]' oder wenn Sie numpy> 1.10 '[np. Stapel ([X, Y, Z], Achse = 1)] 'oder' [np.stack ([X, Y, Z], Achse = 1)] ' – Y0da

Antwort

43

Ich glaube, Sie haben es fast bekam. Ist das was du willst?

from mpl_toolkits.mplot3d import Axes3D 
from mpl_toolkits.mplot3d.art3d import Poly3DCollection 
import matplotlib.pyplot as plt 
fig = plt.figure() 
ax = Axes3D(fig) 
x = [0,1,1,0] 
y = [0,0,1,1] 
z = [0,1,0,1] 
verts = [zip(x, y,z)] 
ax.add_collection3d(Poly3DCollection(verts)) 
plt.show() 

alt text könnten Sie auch art3d.pathpatch_2d_to_3d interessiert sein.

+0

Ich danke Ihnen sehr herzlich. Das ist genau meine Antwort. Eigentlich muss ich Bruchflächen in 3D simulieren. So war es die Grundform. Es sollte dann einfach für mich sein, mit tausenden realistischen Koordinaten zu arbeiten. Ich danke Ihnen nochmals für Ihre großartige Hilfe. Fristal – Developer

+0

Wie man es nicht geschlossen macht? – Dims