From b83977f46806b35e258413db63618b58e2ef0d02 Mon Sep 17 00:00:00 2001 From: Benjamin Root Date: Fri, 28 Jun 2013 18:55:21 -0400 Subject: [PATCH] Much better alpha control in mplot3d. Closes #1541 Probably haven't solved them all, but this gets most of them. --- lib/mpl_toolkits/mplot3d/art3d.py | 36 ++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/mpl_toolkits/mplot3d/art3d.py b/lib/mpl_toolkits/mplot3d/art3d.py index 53ba0fe2e559..6b9d2a45eba5 100644 --- a/lib/mpl_toolkits/mplot3d/art3d.py +++ b/lib/mpl_toolkits/mplot3d/art3d.py @@ -9,6 +9,7 @@ ''' from matplotlib import lines, text as mtext, path as mpath, colors as mcolors +from matplotlib import artist from matplotlib.collections import Collection, LineCollection, \ PolyCollection, PatchCollection from matplotlib.cm import ScalarMappable @@ -318,9 +319,13 @@ def do_3d_projection(self, renderer): xs, ys, zs = self._offsets3d vxs, vys, vzs, vis = proj3d.proj_transform_clip(xs, ys, zs, renderer.M) #FIXME: mpl allows us no way to unset the collection alpha value - self._alpha = None - self.set_facecolors(zalpha(self._facecolor3d, vzs)) - self.set_edgecolors(zalpha(self._edgecolor3d, vzs)) + #self._alpha = None + fcs = mcolors.colorConverter.to_rgba_array(self._facecolor3d, + self._alpha) + mcs = mcolors.colorConverter.to_rgba_array(self._edgecolor3d, + self._alpha) + self.set_facecolors(zalpha(fcs, vzs)) + self.set_edgecolors(zalpha(mcs, vzs)) PatchCollection.set_offsets(self, zip(vxs, vys)) if vzs.size > 0 : @@ -427,6 +432,7 @@ def set_3d_properties(self): self.set_zsort(True) self._facecolors3d = PolyCollection.get_facecolors(self) self._edgecolors3d = PolyCollection.get_edgecolors(self) + self._alpha3d = PolyCollection.get_alpha(self) def set_sort_zpos(self,val): '''Set the position to use for z-sorting.''' @@ -495,6 +501,30 @@ def set_edgecolor(self, colors): self._edgecolors3d = PolyCollection.get_edgecolor(self) set_edgecolors = set_edgecolor + def set_alpha(self, alpha): + """ + Set the alpha tranparencies of the collection. *alpha* must be + a float or *None*. + + ACCEPTS: float or None + """ + if alpha is not None: + try: + float(alpha) + except TypeError: + raise TypeError('alpha must be a float or None') + artist.Artist.set_alpha(self, alpha) + try: + self._facecolors = mcolors.colorConverter.to_rgba_array( + self._facecolors3d, self._alpha) + except (AttributeError, TypeError, IndexError): + pass + try: + self._edgecolors = mcolors.colorConverter.to_rgba_array( + self._edgecolors3d, self._alpha) + except (AttributeError, TypeError, IndexError): + pass + def get_facecolors(self): return self._facecolors2d get_facecolor = get_facecolors