Skip to content

[ENH] Implement dynamic clipping to axes limits for 3D plots #27349

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 4 commits into from
Oct 2, 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
2 changes: 1 addition & 1 deletion doc/missing-references.json
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@
"lib/matplotlib/quiver.py:docstring of matplotlib.quiver.Barbs:212",
"lib/matplotlib/quiver.py:docstring of matplotlib.quiver.Quiver:251",
"lib/mpl_toolkits/mplot3d/art3d.py:docstring of matplotlib.artist.Path3DCollection.set:46",
"lib/mpl_toolkits/mplot3d/art3d.py:docstring of matplotlib.artist.Poly3DCollection.set:44"
"lib/mpl_toolkits/mplot3d/art3d.py:docstring of matplotlib.artist.Poly3DCollection.set:45"
],
"matplotlib.collections._MeshData.set_array": [
"lib/matplotlib/axes/_axes.py:docstring of matplotlib.axes._axes.Axes.pcolormesh:164",
Expand Down
30 changes: 30 additions & 0 deletions doc/users/next_whats_new/3d_clip_to_axis_limits.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Data in 3D plots can now be dynamically clipped to the axes view limits
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

All 3D plotting functions now support the *axlim_clip* keyword argument, which
will clip the data to the axes view limits, hiding all data outside those
bounds. This clipping will be dynamically applied in real time while panning
and zooming.

Please note that if one vertex of a line segment or 3D patch is clipped, then
the entire segment or patch will be hidden. Not being able to show partial
lines or patches such that they are "smoothly" cut off at the boundaries of the
view box is a limitation of the current renderer.

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

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)

# 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))
ax.legend(['axlim_clip=False (default)', 'axlim_clip=True'])
31 changes: 31 additions & 0 deletions galleries/examples/mplot3d/axlim_clip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
=====================================
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.
"""

import matplotlib.pyplot as plt
import numpy as np

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

# Generate the random data
np.random.seed(1)
xyz = np.random.rand(25, 3)

# Default behavior is axlim_clip=False
ax.plot(xyz[:, 0], xyz[:, 1], xyz[:, 2], '-o')

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

plt.show()
2 changes: 2 additions & 0 deletions galleries/users_explain/toolkits/mplot3d.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ See `.Axes3D.fill_between` for API documentation.
:target: /gallery/mplot3d/fillbetween3d.html
:align: center

.. versionadded:: 3.10

.. _polygon3d:

Polygon plots
Expand Down
11 changes: 9 additions & 2 deletions lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,9 +753,16 @@

# don't use self.get_position here, which refers to text
# position in Text:
posx = float(self.convert_xunits(self._x))
posy = float(self.convert_yunits(self._y))
x, y = self._x, self._y
if np.ma.is_masked(x):
x = np.nan

Check warning on line 758 in lib/matplotlib/text.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/text.py#L758

Added line #L758 was not covered by tests
if np.ma.is_masked(y):
y = np.nan

Check warning on line 760 in lib/matplotlib/text.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/text.py#L760

Added line #L760 was not covered by tests
Comment on lines +756 to +760
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what this is likely for, but as these conditions appear uncovered by tests, I'm wondering what actually needed this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is covered by ax.text() in test_axlim_clip?

posx = float(self.convert_xunits(x))
posy = float(self.convert_yunits(y))
posx, posy = trans.transform((posx, posy))
if np.isnan(posx) or np.isnan(posy):
return # don't throw a warning here
if not np.isfinite(posx) or not np.isfinite(posy):
_log.warning("posx and posy should be finite values")
return
Expand Down
Loading
Loading