Skip to content

Commit d6d8238

Browse files
committed
Much better alpha control in mplot3d. Closes matplotlib#1541
Probably haven't solved them all, but this gets most of them.
1 parent d189380 commit d6d8238

1 file changed

Lines changed: 33 additions & 3 deletions

File tree

lib/mpl_toolkits/mplot3d/art3d.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from six.moves import zip
1515

1616
from matplotlib import lines, text as mtext, path as mpath, colors as mcolors
17+
from matplotlib import artist
1718
from matplotlib.collections import Collection, LineCollection, \
1819
PolyCollection, PatchCollection, PathCollection
1920
from matplotlib.cm import ScalarMappable
@@ -340,9 +341,13 @@ def do_3d_projection(self, renderer):
340341
xs, ys, zs = self._offsets3d
341342
vxs, vys, vzs, vis = proj3d.proj_transform_clip(xs, ys, zs, renderer.M)
342343
#FIXME: mpl allows us no way to unset the collection alpha value
343-
self._alpha = None
344-
self.set_facecolors(zalpha(self._facecolor3d, vzs))
345-
self.set_edgecolors(zalpha(self._edgecolor3d, vzs))
344+
#self._alpha = None
345+
fcs = mcolors.colorConverter.to_rgba_array(self._facecolor3d,
346+
self._alpha)
347+
mcs = mcolors.colorConverter.to_rgba_array(self._edgecolor3d,
348+
self._alpha)
349+
self.set_facecolors(zalpha(fcs, vzs))
350+
self.set_edgecolors(zalpha(mcs, vzs))
346351
super(self.__class__, self).set_offsets(list(zip(vxs, vys)))
347352

348353
if vzs.size > 0 :
@@ -461,6 +466,7 @@ def set_3d_properties(self):
461466
self.set_zsort(True)
462467
self._facecolors3d = PolyCollection.get_facecolors(self)
463468
self._edgecolors3d = PolyCollection.get_edgecolors(self)
469+
self._alpha3d = PolyCollection.get_alpha(self)
464470

465471
def set_sort_zpos(self,val):
466472
'''Set the position to use for z-sorting.'''
@@ -529,6 +535,30 @@ def set_edgecolor(self, colors):
529535
self._edgecolors3d = PolyCollection.get_edgecolor(self)
530536
set_edgecolors = set_edgecolor
531537

538+
def set_alpha(self, alpha):
539+
"""
540+
Set the alpha tranparencies of the collection. *alpha* must be
541+
a float or *None*.
542+
543+
ACCEPTS: float or None
544+
"""
545+
if alpha is not None:
546+
try:
547+
float(alpha)
548+
except TypeError:
549+
raise TypeError('alpha must be a float or None')
550+
artist.Artist.set_alpha(self, alpha)
551+
try:
552+
self._facecolors = mcolors.colorConverter.to_rgba_array(
553+
self._facecolors3d, self._alpha)
554+
except (AttributeError, TypeError, IndexError):
555+
pass
556+
try:
557+
self._edgecolors = mcolors.colorConverter.to_rgba_array(
558+
self._edgecolors3d, self._alpha)
559+
except (AttributeError, TypeError, IndexError):
560+
pass
561+
532562
def get_facecolors(self):
533563
return self._facecolors2d
534564
get_facecolor = get_facecolors

0 commit comments

Comments
 (0)