Skip to content

Commit dd96cfe

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 dd96cfe

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
@@ -2361,7 +2361,17 @@ 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+
x_is_data, y_is_data = (collection.get_transform()
2367+
.contains_branch_seperately(self.transData))
2368+
ox_is_data, oy_is_data = (collection.get_offset_transform()
2369+
.contains_branch_seperately(self.transData))
2370+
self.update_datalim(
2371+
points,
2372+
updatex=x_is_data or ox_is_data,
2373+
updatey=y_is_data or oy_is_data,
2374+
)
23652375

23662376
self.stale = True
23672377
return collection

lib/matplotlib/tests/test_collections.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ def test_EllipseCollection():
407407
ec = mcollections.EllipseCollection(
408408
ww, hh, aa, units='x', offsets=XY, offset_transform=ax.transData,
409409
facecolors='none')
410+
print(ec.get_offset_transform())
410411
ax.add_collection(ec)
411412
ax.autoscale_view()
412413

@@ -899,17 +900,23 @@ def test_collection_set_array():
899900

900901

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

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])
905+
# sample data to give initial data limits
906+
ax.plot([2, 3, 4], [0.4, 0.6, 0.5])
907+
np.testing.assert_allclose((ax.dataLim.xmin, ax.dataLim.xmax), (2, 4))
908+
data_ymin, data_ymax = ax.dataLim.ymin, ax.dataLim.ymax
907909

908-
f, ax = plt.subplots()
910+
# LineCollection with vertical lines spanning the Axes vertical, using transAxes
911+
x = [1, 2, 3, 4, 5]
912+
vertical_lines = [np.array([[xi, 0], [xi, 1]]) for xi in x]
909913
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.])
914+
ax.add_collection(LineCollection(vertical_lines, transform=trans))
915+
916+
# check that the x data limits are updated to include the LineCollection
917+
np.testing.assert_allclose((ax.dataLim.xmin, ax.dataLim.xmax), (1, 5))
918+
# check that the y data limits are not updated (because they are not transData)
919+
np.testing.assert_allclose((ax.dataLim.ymin, ax.dataLim.ymax), (0.4, 0.6))
913920

914921

915922
def test_singleton_autolim():

0 commit comments

Comments
 (0)