Skip to content

DOC / BUG: Better example for 3D axlim_clip argument #28984

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 2 commits into from
Oct 17, 2024
Merged
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
16 changes: 10 additions & 6 deletions doc/users/next_whats_new/3d_clip_to_axis_limits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@ view box is a limitation of the current renderer.

.. plot::
:include-source: true
:alt: Example of default behavior (left) and axlim_clip=True (right)
:alt: Example of default behavior (blue) and axlim_clip=True (orange)

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
np.random.seed(1)
xyz = np.random.rand(25, 3)
x = np.arange(-5, 5, 0.5)
y = np.arange(-5, 5, 0.5)
X, Y = np.meshgrid(x, y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

# Note that when a line has one vertex outside the view limits, the entire
# line is hidden. The same is true for 3D patches (not shown).
ax.plot(xyz[:, 0], xyz[:, 1], xyz[:, 2], '-o')
ax.plot(xyz[:, 0], xyz[:, 1], xyz[:, 2], '--*', axlim_clip=True)
ax.set(xlim=(0.25, 0.75), ylim=(0, 1), zlim=(0, 1))
# In this example, data where x < 0 or z > 0.5 is clipped.
ax.plot_wireframe(X, Y, Z, color='C0')
ax.plot_wireframe(X, Y, Z, color='C1', axlim_clip=True)
ax.set(xlim=(0, 10), ylim=(-5, 5), zlim=(-1, 0.5))
ax.legend(['axlim_clip=False (default)', 'axlim_clip=True'])
21 changes: 15 additions & 6 deletions galleries/examples/mplot3d/axlim_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,28 @@

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

# Generate the random data
np.random.seed(1)
xyz = np.random.rand(25, 3)
# Make the data
x = np.arange(-5, 5, 0.5)
y = np.arange(-5, 5, 0.5)
X, Y = np.meshgrid(x, y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

# Default behavior is axlim_clip=False
ax.plot(xyz[:, 0], xyz[:, 1], xyz[:, 2], '-o')
ax.plot_wireframe(X, Y, Z, color='C0')

# 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(xyz[:, 0], xyz[:, 1], xyz[:, 2], '--*', axlim_clip=True)
ax.plot_wireframe(X, Y, Z, color='C1', axlim_clip=True)

ax.set(xlim=(0.25, 0.75), ylim=(0, 1), zlim=(-1, 1))
# 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'])

plt.show()

# %%
# .. tags::
# plot-type: 3D,
# level: beginner
2 changes: 1 addition & 1 deletion lib/mpl_toolkits/mplot3d/art3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ def do_3d_projection(self):
all_points = np.ma.vstack(segments)
masked_points = np.ma.column_stack([*_viewlim_mask(*all_points.T,
self.axes)])
segment_lengths = [segment.shape[0] for segment in segments]
segment_lengths = [np.shape(segment)[0] for segment in segments]
segments = np.split(masked_points, np.cumsum(segment_lengths[:-1]))
xyslist = [proj3d._proj_trans_points(points, self.axes.M)
for points in segments]
Expand Down
Loading