Skip to content

Able to give a variable amount of alpha values into set_alpha in collections #6250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/matplotlib/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,13 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True):
x = ma.asarray(x)
if norm:
x = self.norm(x)

np_alpha = ma.asarray(alpha)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want a masked array here.

if np_alpha.ndim == 1 and np_alpha.shape == x.shape:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not allow passing in a 2D array of alpha for an image?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hadn't considered that. Let me get something working.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you can just remove the dimension check; the shape check is enough.

rgba = self.cmap(x, bytes=bytes)
rgba[0:, 3] = np_alpha
return rgba

rgba = self.cmap(x, alpha=alpha, bytes=bytes)
return rgba

Expand Down
5 changes: 5 additions & 0 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,11 @@ def set_alpha(self, alpha):
ACCEPTS: float or None
"""
if alpha is not None:
# 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:
Expand Down