Skip to content

Commit 8e3b856

Browse files
committed
FIX Update Axes limits from Axes.add_collection(... autolim=True)
... the update now happens separately for both directions, and only if that direction uses data coordinates. Previously, limits were always recalculated for both directions. Closes matplotlib#30320.
1 parent 2b75c9e commit 8e3b856

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2361,7 +2361,11 @@ def add_collection(self, collection, autolim=True):
23612361
# the call so that self.dataLim will update its own minpos.
23622362
# This ensures that log scales see the correct minimum.
23632363
points = np.concatenate([points, [datalim.minpos]])
2364-
self.update_datalim(points)
2364+
# only update the dataLim for x/y if the collection uses transData
2365+
# in this direction.
2366+
updatex, updatey = (collection.get_transform()
2367+
.contains_branch_seperately(self.transData))
2368+
self.update_datalim(points, updatex=updatex, updatey=updatey)
23652369

23662370
self.stale = True
23672371
return collection

lib/matplotlib/tests/test_collections.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -899,17 +899,23 @@ def test_collection_set_array():
899899

900900

901901
def test_blended_collection_autolim():
902-
a = [1, 2, 4]
903-
height = .2
902+
f, ax = plt.subplots()
904903

905-
xy_pairs = np.column_stack([np.repeat(a, 2), np.tile([0, height], len(a))])
906-
line_segs = xy_pairs.reshape([len(a), 2, 2])
904+
# sample data to give initial data limits
905+
ax.plot([2, 3, 4], [0.4, 0.6, 0.5])
906+
np.testing.assert_allclose((ax.dataLim.xmin, ax.dataLim.xmax), (2, 4))
907+
data_ymin, data_ymax = ax.dataLim.ymin, ax.dataLim.ymax
907908

908-
f, ax = plt.subplots()
909+
# LineCollection with vertical lines spanning the Axes vertical, using transAxes
910+
x = [1, 2, 3, 4, 5]
911+
vertical_lines = [np.array([[xi, 0], [xi, 1]]) for xi in x]
909912
trans = mtransforms.blended_transform_factory(ax.transData, ax.transAxes)
910-
ax.add_collection(LineCollection(line_segs, transform=trans))
911-
ax.autoscale_view(scalex=True, scaley=False)
912-
np.testing.assert_allclose(ax.get_xlim(), [1., 4.])
913+
ax.add_collection(LineCollection(vertical_lines, transform=trans))
914+
915+
# check that the x data limits are updated to include the LineCollection
916+
np.testing.assert_allclose((ax.dataLim.xmin, ax.dataLim.xmax), (1, 5))
917+
# check that the y data limits are not updated (because they are not transData)
918+
np.testing.assert_allclose((ax.dataLim.ymin, ax.dataLim.ymax), (0.4, 0.6))
913919

914920

915921
def test_singleton_autolim():

0 commit comments

Comments
 (0)