Matplotlib 3D limitations, please recommend alternative

John Ladasky john_ladasky at sbcglobal.net
Wed Jul 4 17:53:06 EDT 2018


I'm a regular Matplotlib user.  Normally, I graph functions.  I just attempted to graph an icosahedral surface using the plot_trisurf() methods of Matplotlib's Axes3D. I have discovered that Matplotlib is basically hard-wired for graphing functions, and therefore will not work for general-purpose 3D rendering.

If I have repeated X and Y values in my arrays, it doesn't matter that the Z values might be different.  Matplotlib raises a ValueError, with the message "x and y arrays must consist of at least 3 unique points."  If I break down my polyhedron into individual triangles, I can get 16 of the 20 faces to render, but not all of them.  Here's some minimal example code, which also catches and prints the cause of the ValueError.

# ============================================================================

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

COLOR = cycle("cmyrbg")

ϕ = (1 + np.sqrt(5)) / 2

R = np.sqrt(1 + ϕ**2)

VERTICES = np.array(((-1,0,ϕ), (1,0,ϕ), (-1,0,-ϕ), (1,0,-ϕ),
                     (0,ϕ,1), (0,ϕ,-1), (0,-ϕ,1), (0,-ϕ,-1),
                     (ϕ,1,0), (-ϕ,1,0), (ϕ,-1,0), (-ϕ,-1,0))) / R

FACES = np.array(((0,4,1), (4,5,8), (4,8,1), (5,3,8),
                  (0,9,4), (9,5,4), (9,2,5), (5,2,3),
                  (9,0,11), (9,11,2), (7,2,11), (2,7,3),
                  (11,0,6), (7,11,6), (7,6,10), (7,10,3),
                  (0,1,6), (8,10,1), (6,1,10), (8,3,10)))

fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection='3d')
for t in FACES:
    print("\n\n", t, end=" ")
    try:
        ax.plot_trisurf(*zip(*VERTICES[t]), color=next(COLOR), alpha=0.5)
    except ValueError as e:
        print(e)
        print(VERTICES[t], end="")
plt.show()


# ============================================================================

I explored Python OpenGL bindings about three years ago, and quickly got bogged down.  Even with Python to assist, dealing with OpenGL was like trying to program Java.  Of course, OpenGL can do EVERYTHING.  Far more than I need.

I would like to render polyhedra, preferably with uniformly colored faces (I understand that color is a property that is associated with vertices in OpenGL).  I would appreciate a simple interface.  The rendering doesn't have to be especially fast or high quality.  The Matplotlib visualization is acceptable, except of course for the missing faces.

There are many 3D graphics packages on PyPI.  Some appear to be quite specialized.  I would appreciate your recommendations.  Thanks!


More information about the Python-list mailing list