-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
DOC: Add an example for 2D images in 3D plots #28280
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
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
""" | ||
=============== | ||
2D images in 3D | ||
=============== | ||
|
||
This example demonstrates how to plot 2D color coded images (similar to | ||
`.Axes.imshow`) as a plane in 3D. | ||
|
||
Matplotlib does not have a native function for this. Below we build one by relying | ||
on `.Axes3D.plot_surface`. For simplicity, there are some differences to | ||
`.Axes.imshow`: This function does not set the aspect of the Axes, hence pixels are | ||
not necessarily square. Also, pixel edges are on integer values rather than pixel | ||
centers. Furthermore, many optional parameters of `.Axes.imshow` are not implemented. | ||
|
||
Multiple calls of ``imshow3d`` use independent norms and thus different color scales | ||
by default. If you want to have a single common color scale, you need to construct | ||
a suitable norm beforehand and pass it to all ``imshow3d`` calls. | ||
|
||
A fundamental limitation of the 3D plotting engine is that intersecting objects cannot | ||
be drawn correctly. One object will always be drawn after the other. Therefore, | ||
multiple image planes can well be used in the background as shown in this example. | ||
But this approach is not suitable if the planes intersect. | ||
""" | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
from matplotlib.colors import Normalize | ||
|
||
|
||
def imshow3d(ax, array, value_direction='z', pos=0, norm=None, cmap=None): | ||
""" | ||
Display a 2D array as a color-coded 2D image embedded in 3d. | ||
|
||
The image will be in a plane perpendicular to the coordinate axis *value_direction*. | ||
|
||
Parameters | ||
---------- | ||
ax : Axes3D | ||
The 3D Axes to plot into. | ||
array : 2D numpy array | ||
The image values. | ||
value_direction : {'x', 'y', 'z'} | ||
The axis normal to the image plane. | ||
pos : float | ||
The numeric value on the *value_direction* axis at which the image plane is | ||
located. | ||
norm : `~matplotlib.colors.Normalize`, default: Normalize | ||
The normalization method used to scale scalar data. See `imshow()`. | ||
cmap : str or `~matplotlib.colors.Colormap`, default: :rc:`image.cmap` | ||
The Colormap instance or registered colormap name used to map scalar data | ||
to colors. | ||
""" | ||
if norm is None: | ||
norm = Normalize() | ||
colors = plt.get_cmap(cmap)(norm(array)) | ||
|
||
if value_direction == 'x': | ||
nz, ny = array.shape | ||
zi, yi = np.mgrid[0:nz + 1, 0:ny + 1] | ||
xi = np.full_like(yi, pos) | ||
elif value_direction == 'y': | ||
nx, nz = array.shape | ||
xi, zi = np.mgrid[0:nx + 1, 0:nz + 1] | ||
yi = np.full_like(zi, pos) | ||
elif value_direction == 'z': | ||
ny, nx = array.shape | ||
yi, xi = np.mgrid[0:ny + 1, 0:nx + 1] | ||
zi = np.full_like(xi, pos) | ||
else: | ||
raise ValueError(f"Invalid value_direction: {value_direction!r}") | ||
ax.plot_surface(xi, yi, zi, rstride=1, cstride=1, facecolors=colors, shade=False) | ||
|
||
|
||
fig = plt.figure() | ||
ax = fig.add_subplot(projection='3d') | ||
ax.set(xlabel="x", ylabel="y", zlabel="z") | ||
|
||
nx, ny, nz = 8, 10, 5 | ||
data_xy = np.arange(ny * nx).reshape(ny, nx) + 15 * np.random.random((ny, nx)) | ||
data_yz = np.arange(nz * ny).reshape(nz, ny) + 10 * np.random.random((nz, ny)) | ||
data_zx = np.arange(nx * nz).reshape(nx, nz) + 8 * np.random.random((nx, nz)) | ||
|
||
imshow3d(ax, data_xy) | ||
imshow3d(ax, data_yz, value_direction='x', cmap='magma') | ||
imshow3d(ax, data_zx, value_direction='y', pos=ny, cmap='plasma') | ||
|
||
plt.show() | ||
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. 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 slightly favor leaving it as is:
|
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.
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 is a good paragraph to include, this "2.5D" renderer behavior is non-obvious, and we should mention it as often as possible IMO.
Uh oh!
There was an error while loading. Please reload this page.
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.
Yeah, I also think this is great - it explains why this needs to be a separate example and so will be nice to link to off the intersecting example (and they can all have the same tag https://matplotlib.org/devdocs/devel/tag_guidelines.html )