From c6b0c53071781364153f8f66424dd47da8d1985f Mon Sep 17 00:00:00 2001 From: Paul Kirow Date: Fri, 1 Apr 2016 20:07:32 -0400 Subject: [PATCH 1/3] Allows for the a 1D array to be given for alpha when using a color map. --- lib/matplotlib/cm.py | 11 +++++++++++ lib/matplotlib/collections.py | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/matplotlib/cm.py b/lib/matplotlib/cm.py index 6ed13cd2f41d..a87b9fc6e6fb 100644 --- a/lib/matplotlib/cm.py +++ b/lib/matplotlib/cm.py @@ -275,6 +275,17 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True): x = ma.asarray(x) if norm: x = self.norm(x) + + try: + alpha = ma.asarray(alpha) + if alpha.ndim == 1 and alpha.shape == x.shape: + rgba = self.cmap(x, bytes=bytes) + rgba[0:, 3] = alpha + return rgba + except AttributeError: + # e.g., alpha is not an array; + pass + rgba = self.cmap(x, alpha=alpha, bytes=bytes) return rgba diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index 5576def9d440..a62f8dba53a6 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -678,6 +678,14 @@ def set_alpha(self, alpha): ACCEPTS: float or None """ if alpha is not None: + try: + # Check if alpha is an array of matching size + alpha = ma.asarray(alpha) + if alpha.ndim == 1: + artist.Artist.set_alpha(self, alpha) + return + except AttributeError: + pass try: float(alpha) except TypeError: From 9fcef285fe095c48e897ab06ee9d8be9c8036f1a Mon Sep 17 00:00:00 2001 From: Paul Kirow Date: Fri, 1 Apr 2016 20:27:08 -0400 Subject: [PATCH 2/3] Fix for case where no alpha is given. --- lib/matplotlib/cm.py | 6 +++--- lib/matplotlib/collections.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/cm.py b/lib/matplotlib/cm.py index a87b9fc6e6fb..3db149ddd223 100644 --- a/lib/matplotlib/cm.py +++ b/lib/matplotlib/cm.py @@ -277,10 +277,10 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True): x = self.norm(x) try: - alpha = ma.asarray(alpha) - if alpha.ndim == 1 and alpha.shape == x.shape: + np_alpha = ma.asarray(alpha) + if np_alpha.ndim == 1 and np_alpha.shape == x.shape: rgba = self.cmap(x, bytes=bytes) - rgba[0:, 3] = alpha + rgba[0:, 3] = np_alpha return rgba except AttributeError: # e.g., alpha is not an array; diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index a62f8dba53a6..ead604ff09b7 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -680,9 +680,9 @@ def set_alpha(self, alpha): if alpha is not None: try: # Check if alpha is an array of matching size - alpha = ma.asarray(alpha) - if alpha.ndim == 1: - artist.Artist.set_alpha(self, alpha) + np_alpha = ma.asarray(alpha) + if np_alpha.ndim == 1: + artist.Artist.set_alpha(self, np_alpha) return except AttributeError: pass From 0dbd9417d22593e3cd40ad5f886dd718012faf12 Mon Sep 17 00:00:00 2001 From: Paul Kirow Date: Fri, 1 Apr 2016 21:23:40 -0400 Subject: [PATCH 3/3] Removed try-except that are not needed --- lib/matplotlib/cm.py | 14 +++++--------- lib/matplotlib/collections.py | 13 +++++-------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/lib/matplotlib/cm.py b/lib/matplotlib/cm.py index 3db149ddd223..081f5415ab8d 100644 --- a/lib/matplotlib/cm.py +++ b/lib/matplotlib/cm.py @@ -276,15 +276,11 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True): if norm: x = self.norm(x) - try: - np_alpha = ma.asarray(alpha) - if np_alpha.ndim == 1 and np_alpha.shape == x.shape: - rgba = self.cmap(x, bytes=bytes) - rgba[0:, 3] = np_alpha - return rgba - except AttributeError: - # e.g., alpha is not an array; - pass + np_alpha = ma.asarray(alpha) + if np_alpha.ndim == 1 and np_alpha.shape == x.shape: + rgba = self.cmap(x, bytes=bytes) + rgba[0:, 3] = np_alpha + return rgba rgba = self.cmap(x, alpha=alpha, bytes=bytes) return rgba diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index ead604ff09b7..7855f69ff404 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -678,14 +678,11 @@ def set_alpha(self, alpha): ACCEPTS: float or None """ if alpha is not None: - try: - # Check if alpha is an array of matching size - np_alpha = ma.asarray(alpha) - if np_alpha.ndim == 1: - artist.Artist.set_alpha(self, np_alpha) - return - except AttributeError: - pass + # Check if alpha is an array of matching size + np_alpha = ma.asarray(alpha) + if np_alpha.ndim == 1: + artist.Artist.set_alpha(self, np_alpha) + return try: float(alpha) except TypeError: