Skip to content

Commit ef3faf2

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 #30320.
1 parent 2b75c9e commit ef3faf2

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
@@ -899,17 +899,24 @@ 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),
919+
(data_ymin, data_ymax))
913920

914921

915922
def test_singleton_autolim():

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy