Skip to content

Commit 750e2f4

Browse files
committed
Merge pull request matplotlib#2417 from fariza/remlim-with-invisible-option
Adding possibility to remove invisible lines and patches from relim.
2 parents 5c6f2a1 + 94bdfd6 commit 750e2f4

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,9 +1590,11 @@ def add_container(self, container):
15901590
container.set_remove_method(lambda h: self.containers.remove(h))
15911591
return container
15921592

1593-
def relim(self):
1593+
def relim(self, visible_only=False):
15941594
"""
1595-
Recompute the data limits based on current artists.
1595+
Recompute the data limits based on current artists. If you want to exclude
1596+
invisible artists from the calculation, set
1597+
`visible_only=True`
15961598
15971599
At present, :class:`~matplotlib.collections.Collection`
15981600
instances are not supported.
@@ -1604,10 +1606,12 @@ def relim(self):
16041606
self.ignore_existing_data_limits = True
16051607

16061608
for line in self.lines:
1607-
self._update_line_limits(line)
1609+
if not visible_only or line.get_visible():
1610+
self._update_line_limits(line)
16081611

16091612
for p in self.patches:
1610-
self._update_patch_limits(p)
1613+
if not visible_only or p.get_visible():
1614+
self._update_patch_limits(p)
16111615

16121616
def update_datalim(self, xys, updatex=True, updatey=True):
16131617
"""

lib/matplotlib/tests/test_axes.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,6 +1737,32 @@ def test_empty_shared_subplots():
17371737
assert y1 >= 6
17381738

17391739

1740+
@cleanup
1741+
def test_relim_visible_only():
1742+
x1 = (0., 10.)
1743+
y1 = (0., 10.)
1744+
x2 = (-10., 20.)
1745+
y2 = (-10., 30.)
1746+
1747+
fig = matplotlib.figure.Figure()
1748+
ax = fig.add_subplot(111)
1749+
ax.plot(x1, y1)
1750+
assert ax.get_xlim() == x1
1751+
assert ax.get_ylim() == y1
1752+
l = ax.plot(x2, y2)
1753+
assert ax.get_xlim() == x2
1754+
assert ax.get_ylim() == y2
1755+
l[0].set_visible(False)
1756+
assert ax.get_xlim() == x2
1757+
assert ax.get_ylim() == y2
1758+
1759+
ax.relim(visible_only=True)
1760+
ax.autoscale_view()
1761+
1762+
assert ax.get_xlim() == x1
1763+
assert ax.get_ylim() == y1
1764+
1765+
17401766
if __name__ == '__main__':
17411767
import nose
17421768
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)