Skip to content

Commit 7e87cb2

Browse files
tacaswellmeeseeksmachine
authored andcommitted
Backport PR matplotlib#30327: FIX Update Axes limits from Axes.add_collection(... autolim=True)
1 parent 8076615 commit 7e87cb2

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2373,7 +2373,17 @@ def add_collection(self, collection, autolim=True):
23732373
# the call so that self.dataLim will update its own minpos.
23742374
# This ensures that log scales see the correct minimum.
23752375
points = np.concatenate([points, [datalim.minpos]])
2376-
self.update_datalim(points)
2376+
# only update the dataLim for x/y if the collection uses transData
2377+
# in this direction.
2378+
x_is_data, y_is_data = (collection.get_transform()
2379+
.contains_branch_seperately(self.transData))
2380+
ox_is_data, oy_is_data = (collection.get_offset_transform()
2381+
.contains_branch_seperately(self.transData))
2382+
self.update_datalim(
2383+
points,
2384+
updatex=x_is_data or ox_is_data,
2385+
updatey=y_is_data or oy_is_data,
2386+
)
23772387

23782388
self.stale = True
23792389
return collection

lib/matplotlib/tests/test_collections.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -877,17 +877,24 @@ def test_collection_set_array():
877877

878878

879879
def test_blended_collection_autolim():
880-
a = [1, 2, 4]
881-
height = .2
880+
f, ax = plt.subplots()
882881

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

886-
f, ax = plt.subplots()
887+
# LineCollection with vertical lines spanning the Axes vertical, using transAxes
888+
x = [1, 2, 3, 4, 5]
889+
vertical_lines = [np.array([[xi, 0], [xi, 1]]) for xi in x]
887890
trans = mtransforms.blended_transform_factory(ax.transData, ax.transAxes)
888-
ax.add_collection(LineCollection(line_segs, transform=trans))
889-
ax.autoscale_view(scalex=True, scaley=False)
890-
np.testing.assert_allclose(ax.get_xlim(), [1., 4.])
891+
ax.add_collection(LineCollection(vertical_lines, transform=trans))
892+
893+
# check that the x data limits are updated to include the LineCollection
894+
np.testing.assert_allclose((ax.dataLim.xmin, ax.dataLim.xmax), (1, 5))
895+
# check that the y data limits are not updated (because they are not transData)
896+
np.testing.assert_allclose((ax.dataLim.ymin, ax.dataLim.ymax),
897+
(data_ymin, data_ymax))
891898

892899

893900
def test_singleton_autolim():

0 commit comments

Comments
 (0)