Skip to content

Commit 940b897

Browse files
DOC / BUG: Better example for 3D axlim_clip argument (#28984)
* Better example for 3D axlim_clip argument Tags * Bugfix Tests Tests Tests Update lib/mpl_toolkits/mplot3d/art3d.py Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
1 parent 5e5ce02 commit 940b897

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

doc/users/next_whats_new/3d_clip_to_axis_limits.rst

+10-6
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@ view box is a limitation of the current renderer.
1313

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

1818
import matplotlib.pyplot as plt
1919
import numpy as np
2020

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

2528
# Note that when a line has one vertex outside the view limits, the entire
2629
# line is hidden. The same is true for 3D patches (not shown).
27-
ax.plot(xyz[:, 0], xyz[:, 1], xyz[:, 2], '-o')
28-
ax.plot(xyz[:, 0], xyz[:, 1], xyz[:, 2], '--*', axlim_clip=True)
29-
ax.set(xlim=(0.25, 0.75), ylim=(0, 1), zlim=(0, 1))
30+
# In this example, data where x < 0 or z > 0.5 is clipped.
31+
ax.plot_wireframe(X, Y, Z, color='C0')
32+
ax.plot_wireframe(X, Y, Z, color='C1', axlim_clip=True)
33+
ax.set(xlim=(0, 10), ylim=(-5, 5), zlim=(-1, 0.5))
3034
ax.legend(['axlim_clip=False (default)', 'axlim_clip=True'])

galleries/examples/mplot3d/axlim_clip.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,28 @@
1313

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

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

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

2326
# When axlim_clip=True, note that when a line segment has one vertex outside
2427
# the view limits, the entire line is hidden. The same is true for 3D patches
2528
# if one of their vertices is outside the limits (not shown).
26-
ax.plot(xyz[:, 0], xyz[:, 1], xyz[:, 2], '--*', axlim_clip=True)
29+
ax.plot_wireframe(X, Y, Z, color='C1', axlim_clip=True)
2730

28-
ax.set(xlim=(0.25, 0.75), ylim=(0, 1), zlim=(-1, 1))
31+
# In this example, data where x < 0 or z > 0.5 is clipped
32+
ax.set(xlim=(0, 10), ylim=(-5, 5), zlim=(-1, 0.5))
2933
ax.legend(['axlim_clip=False (default)', 'axlim_clip=True'])
3034

3135
plt.show()
36+
37+
# %%
38+
# .. tags::
39+
# plot-type: 3D,
40+
# level: beginner

lib/mpl_toolkits/mplot3d/art3d.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ def do_3d_projection(self):
455455
all_points = np.ma.vstack(segments)
456456
masked_points = np.ma.column_stack([*_viewlim_mask(*all_points.T,
457457
self.axes)])
458-
segment_lengths = [segment.shape[0] for segment in segments]
458+
segment_lengths = [np.shape(segment)[0] for segment in segments]
459459
segments = np.split(masked_points, np.cumsum(segment_lengths[:-1]))
460460
xyslist = [proj3d._proj_trans_points(points, self.axes.M)
461461
for points in segments]

0 commit comments

Comments
 (0)