Description
Trying to create a semitransparent Poly3DCollection
fails as shown in the following.
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 = [list(zip(x, y, z))]
alpha=0.5
fc = "C0"
# This line fails to give a semitransparent artist
pc = Poly3DCollection(verts, alpha = alpha, facecolors=fc, linewidths=1)
ax.add_collection3d(pc)
plt.show()
It gives the following output, which still has an alpha of 1.
Non-Working options
Now one may try several things. The following is not working:
-
Setting facecolor and alpha individually, facecolor first,
pc = Poly3DCollection(verts, linewidths=1) pc.set_facecolor(fc) pc.set_alpha(alpha)
-
Setting facecolors and alpha at instantiation
pc = Poly3DCollection(verts, alpha = alpha, facecolors=fc, linewidths=1)
Working options
However, each of the following is correctly giving a semitransparent artist. This would be the expected outcome for any of the above as well.
-
Setting facecolor and alpha individually, alpha first,
pc = Poly3DCollection(verts, linewidths=1) pc.set_alpha(alpha) # Order reversed pc.set_facecolor(fc)
-
Setting alpha at instantiation, facecolor afterwards
pc = Poly3DCollection(verts, alpha = alpha, linewidths=1) pc.set_facecolor(fc)
-
Setting facecolor and alpha at instantiation
pc = Poly3DCollection(verts, alpha = alpha, facecolor=fc, linewidths=1)
It seems there had been a similar bug in previous versions, https://stackoverflow.com/questions/18897786/transparency-for-poly3dcollection-plot-in-matplotlib which was reported to be fixed in the meantime but might have been reintroduced in a newer version.
In the 2D case the following works as expected and produces a semtransparent artist.
from matplotlib.collections import PolyCollection
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x = [0, 1, 1, 0]
y = [0, 0, 1, 1]
verts = [list(zip(x, y))]
alpha=0.5
fc = "C0"
pc = PolyCollection(verts, alpha = alpha, facecolors=fc, linewidths=1)
ax.add_collection(pc)
ax.autoscale()
plt.show()
[All of the code in this issue has been tested with matplotlib 2.1]