Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Geometric clipping for 3D surface plots
---------------------------------------

`.Axes3D.plot_surface` now supports ``axlim_clip_mode="clip"``
when ``axlim_clip=True``. This clips surface polygons geometrically
to the axes view-limit box instead of hiding a whole polygon whenever
one of its vertices lies outside the limits.
69 changes: 51 additions & 18 deletions galleries/examples/mplot3d/axlim_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,70 @@
Clip the data to the axes view limits
=====================================

Demonstrate clipping of line and marker data to the axes view limits. The
``axlim_clip`` keyword argument can be used in any of the 3D plotting
functions.
Demonstrate clipping of 3D data to the axes view limits.

Without ``axlim_clip``, data may extend beyond the axes view limits. With
``axlim_clip=True`` and ``axlim_clip_mode="hide"``, surface patches or line
segments with vertices outside the view limits are hidden. With
``axlim_clip_mode="clip"``, they are geometrically clipped to the axes
view-limit box.
"""

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
from matplotlib import cm

fig, axs = plt.subplots(
2, 3,
subplot_kw={"projection": "3d"},
figsize=(10, 7),
layout="constrained",
)

# Make the data
x = np.arange(-5, 5, 0.5)
y = np.arange(-5, 5, 0.5)
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(x, y)
R = np.sqrt(X**2 + Y**2)
R = np.hypot(X, Y)
Z = np.sin(R)

# Default behavior is axlim_clip=False
ax.plot_wireframe(X, Y, Z, color='C0')
xlim = (-3.2, 3.2)
ylim = (-3.0, 2.6)
zlim = (-0.45, 0.85)

# When axlim_clip=True, note that when a line segment has one vertex outside
# the view limits, the entire line is hidden. The same is true for 3D patches
# if one of their vertices is outside the limits (not shown).
ax.plot_wireframe(X, Y, Z, color='C1', axlim_clip=True)
cases = [
("unclipped", dict(axlim_clip=False)),
('axlim_clip_mode="hide"', dict(axlim_clip=True, axlim_clip_mode="hide")),
('axlim_clip_mode="clip"', dict(axlim_clip=True, axlim_clip_mode="clip")),
]

# In this example, data where x < 0 or z > 0.5 is clipped
ax.set(xlim=(0, 10), ylim=(-5, 5), zlim=(-1, 0.5))
ax.legend(['axlim_clip=False (default)', 'axlim_clip=True'])
for ax, (title, clip_kwargs) in zip(axs[0], cases):
ax.plot_surface(
X, Y, Z,
cmap=cm.coolwarm,
linewidth=0,
antialiased=False,
**clip_kwargs,
)
ax.set_title(f"surface\n{title}")
ax.set_xlim(*xlim)
ax.set_ylim(*ylim)
ax.set_zlim(*zlim)

plt.show()
for ax, (title, clip_kwargs) in zip(axs[1], cases):
ax.plot_wireframe(
X, Y, Z,
rstride=2,
cstride=2,
linewidth=0.6,
**clip_kwargs,
)
ax.set_title(f"wireframe\n{title}")
ax.set_xlim(*xlim)
ax.set_ylim(*ylim)
ax.set_zlim(*zlim)

plt.show()
# %%
# .. tags::
# plot-type: 3D,
Expand Down
Loading
Loading