-
Notifications
You must be signed in to change notification settings - Fork 366
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
ENH: enable passing RGB(A) to polormesh #2166
Conversation
Does this also need an update of the One thought for testing this would be to duplicate one of the current tests but use RGBA values from |
Thanks @greglucas, I took a look at the wrapping code and this is clearly more complicated than I first thought! I think I can see how to proceed but not sure how soon I will get to it, so I’ll put this in draft for now. |
Ah, I had not twigged that this uses cartopy/lib/cartopy/mpl/geoaxes.py Lines 1954 to 1964 in c8f1b0f
Does this mean that this basically can't work without changes to |
Ahh, you might be right that we can't quite handle RGBA in the general case yet. I do have an open PR over in MPL to make pcolor more mesh-like, which may help with the pcolor RGBA support as well if you want to try that combination out... matplotlib/matplotlib#25027 |
OK, I see that matplotlib/matplotlib#25027 stops |
Yeah, good call, I wouldn't put too much effort into this yet until that has some consensus on whether the PR is desired or if it goes in a different direction. Feel free to leave comments/review over there as well if you have thoughts for how to improve it! |
5c17100
to
7483a21
Compare
OK this now works with your branch for the cases I have tested. There are some loose ends to tidy up:
But I'm happy I have a working POC, and it probably doesn't make sense to polish this too much until matplotlib/matplotlib#25027 makes it into a release candidate. There were a couple of new test failures when I picked up the latest commit to your branch. I've not looked into those yet, but they were both failing on the assertion of expected output which doesn't always mean something has gone wrong! |
Thanks to @kylehofmann for the latest commit, which handles the RGB case that I had previously neglected. |
24ed4a8
to
1031649
Compare
I think all the functionality is there now. A few things are not yet tested:
@kylehofmann I'm sorry but I changed my mind about supporting the masked case for RGB(A). As far as I can tell Matplotlib's pcolormesh just ignores masks on RGB(A) arrays, and I think it makes sense to be consistent with that. Dropping support for that case also made the code simpler. |
I agree, the masked RGBA case should follow Matplotlib. It explicitly states the mask gets ignored in the docstring of to_rgba: With that being said, I think it would be fairly straight forward to implement handling of the masked arrays if we wanted to make them fully transparent? Something like the following patch. index daefbe08e9..0c4a99496a 100644
--- a/lib/matplotlib/cm.py
+++ b/lib/matplotlib/cm.py
@@ -493,6 +493,9 @@ class ScalarMappable:
else:
raise ValueError("Image RGB array must be uint8 or "
"floating point; found %s" % xx.dtype)
+ # Account for any masked entries in the original array
+ if np.ma.is_masked(x):
+ xx[np.any(np.ma.getmaskarray(x), axis=2), 4] = 0
return xx
except AttributeError:
# e.g., x is not an ndarray; so try mapping it |
@greglucas thanks, I was just wondering about that. With your mpl branch, |
@rcomer: No offense taken! Consistency with matplotlib is important, and as you say, the code is simpler. (A big plus!) My only suggestion is to change |
@@ -578,6 +633,8 @@ def test_pcolormesh_diagonal_wrap(): | |||
assert hasattr(mesh, "_wrapped_collection_fix") | |||
|
|||
|
|||
@pytest.mark.skipif(MPL_VERSION.release[:2] >= (3, 8), | |||
reason='redundant from mpl v3.8') |
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.
Have I understood this right? I think this test relates to the need to initially call pcolor
with an array of zeros, which will no longer be necessary with the new PolyQuadMesh
array handling.
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 think that the number of paths should still be short in the new version, so I think this should still be tested. i.e. we only want to draw the small number of cells, we don't want to draw all pcolor cells and make them invisible.
Thanks to matplotlib/matplotlib#26096, Matplotlib's |
459bbb9
to
afbb531
Compare
I think this one is finally ready for review! |
4118598
to
7695be5
Compare
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.
This is really nice work @rcomer! Thank you for tackling this because there are a lot of paths to keep track of with the different versions hanging around everywhere. Eventually, this will slim down the code ;)
@@ -578,6 +633,8 @@ def test_pcolormesh_diagonal_wrap(): | |||
assert hasattr(mesh, "_wrapped_collection_fix") | |||
|
|||
|
|||
@pytest.mark.skipif(MPL_VERSION.release[:2] >= (3, 8), | |||
reason='redundant from mpl v3.8') |
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 think that the number of paths should still be short in the new version, so I think this should still be tested. i.e. we only want to draw the small number of cells, we don't want to draw all pcolor cells and make them invisible.
That test now doesn't get any paths back, so I have updated to assert that from v3.8. But I think I am still not understanding quite right. |
I think you actually had a better grasp on this than I did! I think you can actually skip this after 3.8 like you had before because it doesn't make sense. Let me try and write down what I think is happening and see if you agree. This test passes in one all-nan cell that spans the wrap-line. That actually becomes two wrapped cells in the pcolor-collection. Before MPL3.8 we wanted those cells to show up because we needed to be able to update them later. After MPL3.8 the PolyQuadMesh collection takes care of the shrink/expansion of the paths automatically for us. So, with nan's it is empty, but if we updated the mesh we'd expect two paths to be present ( |
Yes, I think we're on the same page. It makes sense to me to always make sure the wrapped cells can be updated, so I have added that extra assertion. |
Thanks for all your guidance and mpl updates @greglucas! |
Rationale
Since Matplotlib v3.7.0,
pcolormesh
accepts RGBA arrayshttps://matplotlib.org/stable/users/prev_whats_new/whats_new_3.7.0.html#pcolormesh-accepts-rgb-a-colors
Cartopy just needs a small tweak to be able to pass these down, and we can adapt this StackOverflow question to give
Closes #2156 and I think also resolves #2045 and resolves #2171, as the request for
set_array
to handleNone
seems to be a workaround that is not needed since mpl 3.7. I also note that theNone
handling is not actually documented inQuadMesh.set_array
.This also adds test coverage for and fixes #2208.
I'm not sure of the best way to test this. Does it just need a smoke test of apcolormesh
plot with suitable data?Implications