Unfortunately, both the answer and update code result in visualizations that are incorrect, but for different reasons. Explanations are provided below.
A Solution
First consider a 'correct' answer. Matplotlib will correctly plot filled polygons. Polygons are planar. The four example vertices in the question are not planar. But these vertices can be used to define two triangles. The verts
parameter in the art3d.Poly3DCollection is a list of (N, 3) array-like sequence of polygons. Using a list of the four vertices, v, and a list of vertex indices for two triangular faces, f, a simple solution is:
import pylab as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
v = [ [0,0,0], [1,0,1], [1,1,0], [0,1,1] ] # 4 vertices
f = [ [0,1,3], [1,2,3] ] # 2 faces
verts = [ [ v[i] for i in p] for p in f] # list comprehension
c = ['C0','C3'] # 2 colors
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.set(xlim=(0,1), ylim=(0,1), zlim=(0,1))
ax.add_collection3d(Poly3DCollection(verts,color=c))
plt.show()
which produces a figure of two polygons with different colors as:
This even works for a list of polygons with a mixed number of sides. For example, a collection of square and triangular faces:
The code to produce this figure is:
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
z = 2**(1/2)
v = [ [1,1, 1], [-1,1, 1], [-1,-1, 1], [1,-1, 1],
[z,0,-1], [ 0,z,-1], [-z, 0,-1], [0,-z,-1] ] # 8 vertices
f4 = [ [0,1,2,3], [7,6,5,4] ]
fa = [ [0,3,4], [1,0,5], [2,1,6], [3,2,7] ]
fb = [ [4,5,0], [5,6,1], [6,7,2], [7,4,3] ]
f = f4 + fa + fb # 10 faces
verts = [ [ v[i] for i in p] for p in f] # list comprehension
colors = ['teal']*2 + ['indianred']*4 + ['olivedrab']*4 # 10 colors
fig = plt.figure(figsize=plt.figaspect(1))
ax = plt.axes(projection='3d')
ax.set(xlim=(-1.5,1.5), ylim=(-1.5,1.5), zlim=(-1.5,1.5))
ax.add_collection3d(Poly3DCollection(verts,color=colors))
fig.tight_layout()
plt.show()
Notice that the colors in the colors
list are in the same order as the list of faces in the f
list. Also, if all the polygons are of the same type, for example all triangles or all quadrilaterals, it is convenient to use Numpy arrays for the vertices, v, and faces, f. Then the verts
can be simply defined as:
verts = v[f]
instead of using Python list comprehension. In addition, if shading is used, all polygons must be defined with the list of vertex indices for each face consistent with a right-handed or left-handed vertex order.
Problem 1. Non-planar faces
Defining the verts list by:
x = [0,1,1,0]
y = [0,0,1,1]
z = [0,1,0,1]
verts = [list(zip(x,y,z))]
results in defining one four-sided face. The visualization 'appears' correct for the Matplotlib default axes view with azim=-60. However, the visual interpretation is inconsistent for different views. This is shown below by comparing three views. This is not a Matplotlib problem but a problem of incorrectly defining a 4-sided polygon using four vertices which are not in the same plane.
Using the two triangular face solution, the visualization of this 3D collection is consistent for any viewing direction:
A warped surface can be constructed from the four vertices, but this requires subdividing the surface into multiple polygons. Using the Matplotlib third-party package S3Dlib, the following ruled surface was constructed from 512 triangles.
In this case, the top and bottom of the surface was colored blue and red, respectively. Then shading was applied to enhance the visualization of the surface. No longer simple but correct.
Matplotlib 3D visualizations can handle a polycollection of multiple polygons by correctly stacking the polygons along the view direction. However, Matplotlib cannot directly handle the visualization when polygons intersect. Visualizations will be incorrect and dependent on the view.
For example, consider three triangles defined as:
v = [ [0,0,0], [1,0,0], [1,1,0], [0,1,0],
[0,0,1], [1,0,1], [1,1,1], [0,1,1] ] # 8 vertices
f = [ [1,6,4], [1,3,5], [0,2,7] ] # 3 triangles
c = [ 'C0', 'C1', 'C2' ]
These are three intersecting triangles, but when viewed from different orientations, the visualizations are inconsistent.
By subdividing each of the three triangles into smaller triangles, the intersections among the three triangles can be consistently visualized independent of the view:
The above figure of intersecting triangles was created with Matplotlib using S3Dlib.
The question update produces a collection of random triangular polygons. However, as demonstrated in the above simple example, such a large number of polygons will be intersecting. As a result, the actual collection will not be correctly displayed without accounting for the intersections.
By closely examining a similar collection of triangles from different orientations, the visualization anomalies can be detected. This is shown in the following figure.
A possible solution could be to subdivide the numerous triangles into smaller polygons to account for the intersections. However this method would result in a significant computational cost. The problem solution is no longer 'simple'.
Summary
Both problems could have been identified by viewing the 3D collection from various viewing directions. That way, during development, the solution method may have been identified as flawed.
As additional note: seed the Numpy random generator prior to sampling, for example using :
np.random.seed(seed)
Doing this allows using the same set of random numbers during code development and verification.