-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Conversation
I think this is too complicated and confusing.
Even with these suggestions, I am hesitant about this. I suspect there are other multi-alpha use cases, and it would be best to identify all of them and handle them consistently. Functionality for modifying alpha in an existing RGBA array probably will need to be factored out so it can be reused. |
There is defiantly complexity here. If I was to make the alpha input more strict by requiring its shape to match the shape of the colors then there would be a less complicated way of doing this. This leaves me unsure of how to handle the case of the user setting edgecolors and facecolors with different sized arrays. Which brings me to kwargs, and I don't think it is a good idea either. A lot of complexity also comes from handling off cases where the colors are set to things like Could you clarify what you mean in your last point about factoring out functionality? Do you mean modifying alpha in [a single] existing RGBA array or many existing RGBA arrays. If the former then that should be accomplished just calling Also do you think I should consider abandoning this all together? |
Factoring: I was thinking of color mapping, which operates on an array but takes only a single alpha value for the whole array. Even in the original issue, #3643, it seems like this is where the real demand is. As noted in comments there, any case in which an explicit list of facecolors or edgecolors is supplied can be handled by the user when generating the list--just supply a list of RGBA. So the one area where functionality could be added is in anything inheriting from ScalarMappable, in cases where the colors are generated by normalizing and mapping an array, |
Taking your suggestions into account, this is what I implemented. Much less complex. For example in examples/shapes_and_collections/artist_reference.py, I can initialize the collection in the following way,
|
With regards to this. |
if np_alpha.ndim == 1: | ||
artist.Artist.set_alpha(self, np_alpha) | ||
return | ||
except AttributeError: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where would the AttributeError
come from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The try-except is likely unnecessary now. I missed this, but I will remove it now.
Can you squash all of the discarded revisions out? |
@@ -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) | |||
if np_alpha.ndim == 1 and np_alpha.shape == x.shape: |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Getting this to work for images seems quite difficult. The difficulty is from when alpha is being applied in _make_image, the image gets resampled and that changes its shape meaning I can't apply the alpha values unless I resample the alpha values also. This is what I had implemented before I realized the color values were given a different size. From the same function L402
|
I don't think that per-pixel-alpha makes sense for images in general. As you have found, it is too complicated--and what's the use case? It might make sense for the case that can be handled by pcolormesh--that is, when there is no resampling or interpolation. |
It seems really odd to me to embed a check like this is cmap that can only be used in some of it's clients. Either |
@tacaswell, I don't understand your either/or above. With the change in this PR, alpha is going into the colormapping--it is applying an array of alpha, if that is supplied, instead of just a scalar value. It isn't presently doing the right kind of shape checking, but it is on the right track. When that is fixed, it should work for all collections, I believe. My point is that I don't think we should try to make it work now for images, which are not collections. They are a different sort of beast, and I think that allowing alpha as an array will require changes in the extension code. Maybe that would be easy for @mdboom, maybe not. |
@@ -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) |
There was a problem hiding this comment.
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.
@tacaswell, Now I think you are suggesting that the application of a varying alpha should be pushed down into the Maybe the best reason for pushing it down is that this would make it possible to keep the alpha array specification from clobbering the handling of "bad" values. Getting all of this right is looking a bit tricky, as usual with alpha handling. |
This direction makes sense to me. Thank you for the feedback 👍 |
From issue #3643
Multiple alpha values can be given to
collection.set_alpha
. The behaviour of this differs depending on what the colour input is.If there is only one face/edge colour then the given alpha values will alternate over all collection items.
If there are more face/edge colours than alphas, set_alpha will apply to as many colours as there are alphas. This is to avoid the situation of (for example) having 6 colours and 5 alphas, then set_alpha would need to create 30 different combinations. I decided against this and instead, in this situation the first 5 colours will have the alpha values applied to them and the sixth color will have no alpha applied to it.