Reputation: 2543
hi I am trying to make 3D
plot with Poly3DCollection
and some how alpha
does not work in defining transparency over here.
Below is my code and result i get from it,
alpha = 0.
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import numpy as np
import matplotlib.pyplot as plt
#limits of the plot
Length =1
radius = (numOfLayer>1)*(np.sqrt(3.)*(numOfLayer-1)-Length)+Length#the radius of circle to be projected on
print radius
#plotting part
fig = plt.figure(frameon=False,figsize=(12,10))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
ax = Axes3D(fig)
ax.set_xlim((-.7*radius,.7*radius))
ax.set_ylim((-.7*radius,.7*radius))
ax.set_zlim((0*radius,1.4*radius))
facecolor = 'b'
verts = [[(-0.6962510224883288, 1.2329327488249058, 4.908612818079117), (-1.0599322764400312, -0.03568680405397959, 5.048184968745244), (-0.6959477536580441, -0.4503841919177401, 5.115949349706102), (0.3405472328494507, -0.6409578506970404, 5.2033038005766), (0.7316884930333567, 0.3578625290610191, 5.107972773928745), (0.5640396552966862, 0.8785593292287033, 5.040485995990843), (-0.6962510224883288, 1.2329327488249058, 4.908612818079117)]]
ax.add_collection3d(Poly3DCollection(verts,alpha = alpha,linewidths=1,edgecolors='k', facecolors = facecolor))
#ax.axis("off")
ax.view_init(azim = 90, elev = 90)
plt.show()
Upvotes: 0
Views: 1233
Reputation: 339150
There seems to be some bug or undesired feature involved here. Lets consider a simplified case.
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()
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)
However, each of the following is correctly giving a semitransparent artist:
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, Transparency for Poly3DCollection plot in matplotlib which was reported to be fixed in the meantime but might have been reintroduced in a newer version.
I reported this on GitHub: https://github.com/matplotlib/matplotlib/issues/10237
Upvotes: 3