Skip to content
Navigation Menu
{{ message }}
-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Multivar imshow #30597
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
Open
trygvrad
wants to merge
10
commits into
matplotlib:main
Choose a base branch
from
trygvrad:multivar_imshow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Multivar imshow #30597
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4a2cfc9
expose multivariate plotting functionality to top level functions ims…
trygvrad ca83ad3
updates from code review
trygvrad 02016b5
Apply suggestions from code review
trygvrad ddcfe37
improved consistency of return types for better typing
trygvrad fce7799
implementing feedback from story645
trygvrad 5393cba
doc fix for _ColorizerInterface.get_clim()
trygvrad b8caed7
update based on feedback from @timhoffm
trygvrad fdb3725
Apply suggestions from code review
trygvrad d49ef95
multivar imshow additional test
trygvrad b701032
updates based on feedback from @timhoffm
trygvrad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6238,6 +6238,9 @@ def imshow(self, X, cmap=None, norm=None, *, aspect=None, | |
| - (M, N): an image with scalar data. The values are mapped to | ||
| colors using normalization and a colormap. See parameters *norm*, | ||
| *cmap*, *vmin*, *vmax*. | ||
| - (K, M, N): a K-component M*N image for multivariate colormapping. | ||
| This must be used with a `.BivarColormap` (K=2) or generally with a | ||
| K-component `.MultivarColormap`. | ||
| - (M, N, 3): an image with RGB values (0-1 float or 0-255 int). | ||
| - (M, N, 4): an image with RGBA values (0-1 float or 0-255 int), | ||
| i.e. including transparency. | ||
|
|
@@ -6247,15 +6250,16 @@ def imshow(self, X, cmap=None, norm=None, *, aspect=None, | |
|
|
||
| Out-of-range RGB(A) values are clipped. | ||
|
|
||
| %(cmap_doc)s | ||
|
|
||
| This parameter is ignored if *X* is RGB(A). | ||
| %(multi_cmap_doc)s | ||
|
|
||
| %(norm_doc)s | ||
| Scalar colormaps are ignored if *X* is RGB(A). | ||
|
|
||
| %(multi_norm_doc)s | ||
|
|
||
| This parameter is ignored if *X* is RGB(A). | ||
|
|
||
| %(vmin_vmax_doc)s | ||
| %(multi_vmin_vmax_doc)s | ||
|
|
||
| This parameter is ignored if *X* is RGB(A). | ||
|
|
||
|
|
@@ -6334,6 +6338,10 @@ def imshow(self, X, cmap=None, norm=None, *, aspect=None, | |
| See :doc:`/gallery/images_contours_and_fields/image_antialiasing` for | ||
| a discussion of image antialiasing. | ||
|
|
||
| When using a `~matplotlib.colors.BivarColormap` or | ||
| `~matplotlib.colors.MultivarColormap`, 'data' is the only valid | ||
| interpolation_stage. | ||
|
|
||
| alpha : float or array-like, optional | ||
| The alpha blending value, between 0 (transparent) and 1 (opaque). | ||
| If *alpha* is an array, the alpha blending values are applied pixel | ||
|
|
@@ -6439,6 +6447,7 @@ def imshow(self, X, cmap=None, norm=None, *, aspect=None, | |
| if aspect is not None: | ||
| self.set_aspect(aspect) | ||
|
|
||
| X = mcolorizer._ensure_multivariate_data(X, im.norm.n_components) | ||
| im.set_data(X) | ||
| im.set_alpha(alpha) | ||
| if im.get_clip_path() is None: | ||
|
|
@@ -6594,9 +6603,10 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None, | |
|
|
||
| Parameters | ||
| ---------- | ||
| C : 2D array-like | ||
| C : 2D (M, N) or 3D (K, M, N) array-like | ||
| The color-mapped values. Color-mapping is controlled by *cmap*, | ||
| *norm*, *vmin*, and *vmax*. | ||
| *norm*, *vmin*, and *vmax*. 3D arrays are supported only if the | ||
| cmap supports K channels. | ||
|
|
||
| X, Y : array-like, optional | ||
| The coordinates of the corners of quadrilaterals of a pcolormesh:: | ||
|
|
@@ -6639,11 +6649,11 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None, | |
| See :doc:`/gallery/images_contours_and_fields/pcolormesh_grids` | ||
| for more description. | ||
|
|
||
| %(cmap_doc)s | ||
| %(multi_cmap_doc)s | ||
|
|
||
| %(norm_doc)s | ||
| %(multi_norm_doc)s | ||
|
|
||
| %(vmin_vmax_doc)s | ||
| %(multi_vmin_vmax_doc)s | ||
|
|
||
| %(colorizer_doc)s | ||
|
|
||
|
|
@@ -6718,8 +6728,19 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None, | |
| if shading is None: | ||
| shading = mpl.rcParams['pcolor.shading'] | ||
| shading = shading.lower() | ||
| X, Y, C, shading = self._pcolorargs('pcolor', *args, shading=shading, | ||
| kwargs=kwargs) | ||
|
|
||
| mcolorizer.ColorizingArtist._check_exclusionary_keywords(colorizer, | ||
| vmin=vmin, vmax=vmax, | ||
| norm=norm, cmap=cmap) | ||
| if colorizer is None: | ||
| colorizer = mcolorizer.Colorizer(cmap=cmap, norm=norm) | ||
|
|
||
| C = mcolorizer._ensure_multivariate_data(args[-1], | ||
| colorizer.cmap.n_variates) | ||
|
Comment on lines
+6738
to
+6739
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel
Good naming is hard and I propose to do this as a follow-up as this is internal and has been here before the PR. |
||
|
|
||
| X, Y, C, shading = self._pcolorargs('pcolor', *args[:-1], C, | ||
| shading=shading, kwargs=kwargs) | ||
|
|
||
| linewidths = (0.25,) | ||
| if 'linewidth' in kwargs: | ||
| kwargs['linewidths'] = kwargs.pop('linewidth') | ||
|
|
@@ -6754,9 +6775,7 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None, | |
| coords = stack([X, Y], axis=-1) | ||
|
|
||
| collection = mcoll.PolyQuadMesh( | ||
| coords, array=C, cmap=cmap, norm=norm, colorizer=colorizer, | ||
| alpha=alpha, **kwargs) | ||
| collection._check_exclusionary_keywords(colorizer, vmin=vmin, vmax=vmax) | ||
| coords, array=C, colorizer=colorizer, alpha=alpha, **kwargs) | ||
| collection._scale_norm(norm, vmin, vmax) | ||
|
|
||
| coords = coords.reshape(-1, 2) # flatten the grid structure; keep x, y | ||
|
|
@@ -6794,6 +6813,9 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None, | |
| - (M, N) or M*N: a mesh with scalar data. The values are mapped to | ||
| colors using normalization and a colormap. See parameters *norm*, | ||
| *cmap*, *vmin*, *vmax*. | ||
| - (K, M, N): a K-component M*N mesh for multivariate colormapping. | ||
| This must be used with a `.BivarColormap` (K=2) or generally with a | ||
| K-component `.MultivarColormap`. | ||
| - (M, N, 3): an image with RGB values (0-1 float or 0-255 int). | ||
| - (M, N, 4): an image with RGBA values (0-1 float or 0-255 int), | ||
| i.e. including transparency. | ||
|
|
@@ -6828,11 +6850,11 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None, | |
| expanded as needed into the appropriate 2D arrays, making a | ||
| rectangular grid. | ||
|
|
||
| %(cmap_doc)s | ||
| %(multi_cmap_doc)s | ||
|
|
||
| %(norm_doc)s | ||
| %(multi_norm_doc)s | ||
|
|
||
| %(vmin_vmax_doc)s | ||
| %(multi_vmin_vmax_doc)s | ||
|
|
||
| %(colorizer_doc)s | ||
|
|
||
|
|
@@ -6956,16 +6978,24 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None, | |
| shading = mpl._val_or_rc(shading, 'pcolor.shading').lower() | ||
| kwargs.setdefault('edgecolors', 'none') | ||
|
|
||
| X, Y, C, shading = self._pcolorargs('pcolormesh', *args, | ||
| mcolorizer.ColorizingArtist._check_exclusionary_keywords(colorizer, | ||
| vmin=vmin, vmax=vmax, | ||
| norm=norm, cmap=cmap) | ||
| if colorizer is None: | ||
| colorizer = mcolorizer.Colorizer(cmap=cmap, norm=norm) | ||
|
|
||
|
story645 marked this conversation as resolved.
|
||
| C = mcolorizer._ensure_multivariate_data(args[-1], | ||
| colorizer.cmap.n_variates) | ||
|
|
||
| X, Y, C, shading = self._pcolorargs('pcolormesh', *args[:-1], C, | ||
| shading=shading, kwargs=kwargs) | ||
| coords = np.stack([X, Y], axis=-1) | ||
|
|
||
| kwargs.setdefault('snap', mpl.rcParams['pcolormesh.snap']) | ||
|
|
||
| collection = mcoll.QuadMesh( | ||
| coords, antialiased=antialiased, shading=shading, | ||
| array=C, cmap=cmap, norm=norm, colorizer=colorizer, alpha=alpha, **kwargs) | ||
| collection._check_exclusionary_keywords(colorizer, vmin=vmin, vmax=vmax) | ||
| array=C, colorizer=colorizer, alpha=alpha, **kwargs) | ||
| collection._scale_norm(norm, vmin, vmax) | ||
|
|
||
| coords = coords.reshape(-1, 2) # flatten the grid structure; keep x, y | ||
|
|
@@ -8911,6 +8941,9 @@ def matshow(self, Z, **kwargs): | |
|
|
||
| """ | ||
| Z = np.asanyarray(Z) | ||
| if Z.ndim != 2: | ||
| if Z.ndim != 3 or Z.shape[2] not in (1, 3, 4): | ||
| raise TypeError(f"Invalid shape {Z.shape} for image data") | ||
| kw = {'origin': 'upper', | ||
| 'interpolation': 'nearest', | ||
| 'aspect': 'equal', # (already the imshow default) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
You can’t perform that action at this time.

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.
multi_cmap_docshould be rewritten fromto
because we should phrase from the perspective of the documented parameter (here: cmap).
Also, it states "This parameter is ignored if X is RGB(A)."
How do we know RGB(A), i.e. shape (M, N, 3) or (M, N, 4) if there is (K, N, M) as multivariate data. Is this now a heuristic that the first or last dimension is low?