In this question, it is shown how filled 3D polygons can be plotted using Poly3DCollection
.
Consider following MWE:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
def drawSquare(xco, yco, zco):
verts = [list(zip(xco, yco, zco))]
temp = Poly3DCollection(verts)
temp.set_facecolor('k')
temp.set_edgecolor('k')
ax.add_collection3d(temp, zs='zco')
fig = plt.figure(1)
plt.clf()
ax = fig.add_subplot(121, projection='3d')
drawSquare([-1, 1, 1, -1], [-1, -1, 1, 1], [1, 1, 1, 1]) #draw top plane
drawSquare([-1, 1, 1, -1], [-1, -1, 1, 1], [-1, -1, -1, -1]) #draw bottom plane
ax.plot3D([-1, 1], [-1, 1], [1, 2], 'red') #draw red line
plt.xlim([-2, 2])
plt.ylim([-2, 2])
ax.set_zlim([-2, 2])
ax = fig.add_subplot(122, projection='3d')
drawSquare([-1, 1, 1, -1], [-1, -1, 1, 1], [1, 1, 1, 1]) #draw top plane
drawSquare([-1, 1, 1, -1], [-1, -1, 1, 1], [-1, -1, -1, -1]) #draw bottom plane
drawSquare([-1, 1, 1, -1], [-1, -1, -1, -1], [1, 1, -1, -1]) #draw side plane 1/4
drawSquare([-1, 1, 1, -1], [1, 1, 1, 1], [1, 1, -1, -1]) #draw side plane 2/4
drawSquare([-1, -1, -1, -1], [-1, 1, 1, -1], [1, 1, -1, -1]) #draw side plane 3/4
drawSquare([1, 1, 1, 1], [-1, 1, 1, -1], [1, 1, -1, -1]) #draw side plane 4/4
ax.plot3D([-1, 1], [-1, 1], [1, 2], 'red') #draw red line
plt.xlim([-2, 2])
plt.ylim([-2, 2])
ax.set_zlim([-2, 2])
plt.show()
which gives this figure:
Why is the red line not entirely visible in the second figure, where more black squares were drawn? In both cases, the red line is the last object to be plotted.
You need zorder
to bring up the line on top. And also some transparencies in the facecolor
('#666666aa' in stead of 'k'
) to make better plot.
ax.plot3D([-1, 1], [-1, 1], [1, 2], 'red', zorder=20)
Sample output: