From c874d13ec2e151b04a37b0b84c42110e660be916 Mon Sep 17 00:00:00 2001 From: Scott Shambaugh <14363975+scottshambaugh@users.noreply.github.com> Date: Wed, 16 Oct 2024 09:18:12 -0600 Subject: [PATCH 1/2] Better example for 3D axlim_clip argument Tags --- .../next_whats_new/3d_clip_to_axis_limits.rst | 16 ++++++++------ galleries/examples/mplot3d/axlim_clip.py | 21 +++++++++++++------ 2 files changed, 25 insertions(+), 12 deletions(-) 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..7ace848fb111 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 will be 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..28634518e0c4 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 will be 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 From d2bf1a809b5d72a987565de387e115556147de9b Mon Sep 17 00:00:00 2001 From: Scott Shambaugh <14363975+scottshambaugh@users.noreply.github.com> Date: Wed, 16 Oct 2024 09:47:11 -0600 Subject: [PATCH 2/2] Bugfix Tests Tests Tests Update lib/mpl_toolkits/mplot3d/art3d.py Co-authored-by: Elliott Sales de Andrade --- doc/users/next_whats_new/3d_clip_to_axis_limits.rst | 8 ++++---- galleries/examples/mplot3d/axlim_clip.py | 8 ++++---- lib/mpl_toolkits/mplot3d/art3d.py | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) 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 7ace848fb111..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 @@ -19,15 +19,15 @@ view box is a limitation of the current renderer. import numpy as np fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) - X = np.arange(-5, 5, 0.5) - Y = np.arange(-5, 5, 0.5) - X, Y = np.meshgrid(X, Y) + 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). - # In this example, data where x < 0 or z > 0.5 will be clipped. + # 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)) diff --git a/galleries/examples/mplot3d/axlim_clip.py b/galleries/examples/mplot3d/axlim_clip.py index 28634518e0c4..2a29f2bf2431 100644 --- a/galleries/examples/mplot3d/axlim_clip.py +++ b/galleries/examples/mplot3d/axlim_clip.py @@ -14,9 +14,9 @@ fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) # Make the data -X = np.arange(-5, 5, 0.5) -Y = np.arange(-5, 5, 0.5) -X, Y = np.meshgrid(X, Y) +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) @@ -28,7 +28,7 @@ # if one of their vertices is outside the limits (not shown). ax.plot_wireframe(X, Y, Z, color='C1', axlim_clip=True) -# In this example, data where x < 0 or z > 0.5 will be clipped. +# 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']) 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]