diff --git a/doc/users/next_whats_new/3d_clip_to_axis_limits.rst b/doc/users/next_whats_new/3d_clip_to_axis_limits.rst index d97ba1b675ba..a2230bddd66c 100644 --- a/doc/users/next_whats_new/3d_clip_to_axis_limits.rst +++ b/doc/users/next_whats_new/3d_clip_to_axis_limits.rst @@ -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']) diff --git a/galleries/examples/mplot3d/axlim_clip.py b/galleries/examples/mplot3d/axlim_clip.py index b25c55a30ad1..2a29f2bf2431 100644 --- a/galleries/examples/mplot3d/axlim_clip.py +++ b/galleries/examples/mplot3d/axlim_clip.py @@ -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 diff --git a/lib/mpl_toolkits/mplot3d/art3d.py b/lib/mpl_toolkits/mplot3d/art3d.py index 74106cfdf91b..0467d2e96e5e 100644 --- a/lib/mpl_toolkits/mplot3d/art3d.py +++ b/lib/mpl_toolkits/mplot3d/art3d.py @@ -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]