-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Multivariate plotting in imshow, pcolor and pcolormesh #29221
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
base: main
Are you sure you want to change the base?
Conversation
Also: this does not (yet) include changes to the docstrings, i.e. the following will need to change:
to include a line like
|
5afd692
to
90680bd
Compare
90680bd
to
206c9b8
Compare
I updated the docstrings, so now this PR should be ready for review. There is an error with the docs on circleci, but I haven't found a link between the error and the changes made in this PR, so I believe it to be unrelated. |
d4215a3
to
e30e7e6
Compare
@story645 also: this will need a squash-merge |
e30e7e6
to
f936378
Compare
# pass scalar data | ||
# and already formatted data | ||
return data | ||
elif data.dtype in [np.complex64, np.complex128]: |
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.
Option 1 please.
if A.ndim == 3 and A.shape[-1] == 1: | ||
A = A.squeeze(-1) # If just (M, N, 1), assume scalar and apply colormap. | ||
if not (A.ndim == 2 or A.ndim == 3 and A.shape[-1] in [3, 4]): | ||
if A.ndim == 3 and A.shape[0] == 2: |
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.
to verify, these are here as a hint to the user to use multi-variate color maps and data that would have failed before would still fail now?
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.
Yes, that is the intention
raise TypeError(f"Image data of dtype {A.dtype} cannot be " | ||
f"converted to float") | ||
else: | ||
for key in A.dtype.fields: |
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 this fails to catch n_input >1
+ non-float-like + non-structured data?
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 might be true.
I'm trying to think of a data type that can not be cast to float and is not structured in order to test this
If you try something with data type object, it depends a bit on the shape, all the cases I have tried have been caught:
fig, ax = plt.subplots()
im = np.empty((5,5), dtype='object')
ax.imshow(im, cmap='BiOrangeBlue')
> ValueError: Invalid data entry for mutlivariate data. The data must contain complex numbers, or have a first dimension 2, or be of a dtype with 2 fields
im = np.empty((2,5,5), dtype='object')
ax.imshow(im, cmap='BiOrangeBlue')
> TypeError: Image data of dtype [('f0', 'O'), ('f1', 'O')] cannot be converted to a sequence of floats
Where the first error originates in colorizer._ensure_multivariate_data()
Do you have some idea of other tests I should run, or do we want to have specific tests for _normalize_image_array()
?
lib/matplotlib/image.py
Outdated
""" | ||
Check validity of image-like input *A* and normalize it to a format suitable for | ||
Image subclasses. | ||
""" | ||
A = mcolorizer._ensure_multivariate_data(n_input, A) |
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.
minor thing. bit jarring that sometimes the API in f(n_input, A)
and othertimes f(A, n_input)
. I think we should prefer f(A, n_input)
everywhere for internal consistency and no-API change on existing functions (like the one we are in now).
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 a good comment :) I will try to address it soon
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 changed the call signature of colorizer._ensure_multivariate_data(...)
to colorizer._ensure_multivariate_data(A, n_input)
I looked to see if there were any other functions with a similar signature which should be flipped, but I did not find any in my quick search.
63e789a
to
7fc99b5
Compare
If you've got the bandwidth, @greglucas can you give this a look (skim) since you've been working on colormapping? |
7fc99b5
to
dc6dee7
Compare
@greglucas It would be great to know if/when you will have time to look at this :) |
@story645 @tacaswell would you two be willing to finish your review of this? EDIT: my apologies @story645 I didn't see you had just assigned this when I wrote my comment :) |
@anntzer asked if the more cleanup/refactor/not directly necessary for implementation parts of this PR could be spun off into a different set and if this PR could get rebased to clean up the commit history. |
@anntzer I'll see what I can do.
I'll also see if there are some misc changes that can be a separate PR between 1 and 2. |
I guess it doesn't really matter, because we know the end result thanks to this PR anyways. |
If an image is shown, and the user hovers over the image, the value at that coordinate should be displayed. The number formating is handled by colorizer.Colorizer._format_cursor_data_override(). This is now updated to also handle the case where a MultiNorm is used (i.e. a bivariat or multivariate colormap), in which case multiple values are displayed.
The previous version of this test passed for numpy 1.x but not for 2.x. This should pass for both
Co-authored-by: Thomas A Caswell <tcaswell@gmail.com>
changed from colorizer._ensure_multivariate_data(n_input, A) to colorizer._ensure_multivariate_data(A, n_input)
29093b3
to
fa40bef
Compare
PR summary
This PR continues the work of #28658 and #28454, aiming to close #14168.
Features included in this PR:
MultiNorm
class. This is a subclass ofcolors.Normalize
and holdsn_variate
norms.MultiNorm
together withBivarColormap
andMultivarColormap
to the plotting functionsaxes.imshow(...)
,axes.pcolor
, and `axes.pcolormesh(...)Features not included in this PR:
fig.colorbar(...)
. I will make a draft for this, but I may need help with regards to the layout engine.fig.colorbar(...)
solution for bivariate/multivariate colormaps.axes.imshow(...)
included here only acceptsinterpolation_stage=='rgba'
, and notinterpolation_stage=='data'
.PR checklist
fig.colorbar(...)
equivalent for multivariate colormaps.