Skip to content

Commit d80342f

Browse files
committed
Don't simplify paths used for autoscaling
Path simplification is really scaled for pixel/display unit outputs, but paths in autoscaling are in data units. This sometimes causes autoscaling to pick the wrong limits, as the simplified paths may be smaller than the originals. Fixes matplotlib#24097
1 parent c744161 commit d80342f

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2434,7 +2434,7 @@ def _update_patch_limits(self, patch):
24342434
# Get all vertices on the path
24352435
# Loop through each segment to get extrema for Bezier curve sections
24362436
vertices = []
2437-
for curve, code in p.iter_bezier():
2437+
for curve, code in p.iter_bezier(simplify=False):
24382438
# Get distance along the curve of any extrema
24392439
_, dzeros = curve.axis_aligned_extrema()
24402440
# Calculate vertices of start, end and any extrema in between

lib/matplotlib/tests/test_axes.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8165,6 +8165,58 @@ def test_bezier_autoscale():
81658165
assert ax.get_ylim()[0] == -0.5
81668166

81678167

8168+
def test_small_autoscale():
8169+
# Check that paths with small values autoscale correctly #24097.
8170+
verts = np.array([
8171+
[-5.45, 0.00], [-5.45, 0.00], [-5.29, 0.00], [-5.29, 0.00],
8172+
[-5.13, 0.00], [-5.13, 0.00], [-4.97, 0.00], [-4.97, 0.00],
8173+
[-4.81, 0.00], [-4.81, 0.00], [-4.65, 0.00], [-4.65, 0.00],
8174+
[-4.49, 0.00], [-4.49, 0.00], [-4.33, 0.00], [-4.33, 0.00],
8175+
[-4.17, 0.00], [-4.17, 0.00], [-4.01, 0.00], [-4.01, 0.00],
8176+
[-3.85, 0.00], [-3.85, 0.00], [-3.69, 0.00], [-3.69, 0.00],
8177+
[-3.53, 0.00], [-3.53, 0.00], [-3.37, 0.00], [-3.37, 0.00],
8178+
[-3.21, 0.00], [-3.21, 0.01], [-3.05, 0.01], [-3.05, 0.01],
8179+
[-2.89, 0.01], [-2.89, 0.01], [-2.73, 0.01], [-2.73, 0.02],
8180+
[-2.57, 0.02], [-2.57, 0.04], [-2.41, 0.04], [-2.41, 0.04],
8181+
[-2.25, 0.04], [-2.25, 0.06], [-2.09, 0.06], [-2.09, 0.08],
8182+
[-1.93, 0.08], [-1.93, 0.10], [-1.77, 0.10], [-1.77, 0.12],
8183+
[-1.61, 0.12], [-1.61, 0.14], [-1.45, 0.14], [-1.45, 0.17],
8184+
[-1.30, 0.17], [-1.30, 0.19], [-1.14, 0.19], [-1.14, 0.22],
8185+
[-0.98, 0.22], [-0.98, 0.25], [-0.82, 0.25], [-0.82, 0.27],
8186+
[-0.66, 0.27], [-0.66, 0.29], [-0.50, 0.29], [-0.50, 0.30],
8187+
[-0.34, 0.30], [-0.34, 0.32], [-0.18, 0.32], [-0.18, 0.33],
8188+
[-0.02, 0.33], [-0.02, 0.32], [0.13, 0.32], [0.13, 0.33], [0.29, 0.33],
8189+
[0.29, 0.31], [0.45, 0.31], [0.45, 0.30], [0.61, 0.30], [0.61, 0.28],
8190+
[0.77, 0.28], [0.77, 0.25], [0.93, 0.25], [0.93, 0.22], [1.09, 0.22],
8191+
[1.09, 0.19], [1.25, 0.19], [1.25, 0.17], [1.41, 0.17], [1.41, 0.15],
8192+
[1.57, 0.15], [1.57, 0.12], [1.73, 0.12], [1.73, 0.10], [1.89, 0.10],
8193+
[1.89, 0.08], [2.05, 0.08], [2.05, 0.07], [2.21, 0.07], [2.21, 0.05],
8194+
[2.37, 0.05], [2.37, 0.04], [2.53, 0.04], [2.53, 0.02], [2.69, 0.02],
8195+
[2.69, 0.02], [2.85, 0.02], [2.85, 0.01], [3.01, 0.01], [3.01, 0.01],
8196+
[3.17, 0.01], [3.17, 0.00], [3.33, 0.00], [3.33, 0.00], [3.49, 0.00],
8197+
[3.49, 0.00], [3.65, 0.00], [3.65, 0.00], [3.81, 0.00], [3.81, 0.00],
8198+
[3.97, 0.00], [3.97, 0.00], [4.13, 0.00], [4.13, 0.00], [4.29, 0.00],
8199+
[4.29, 0.00], [4.45, 0.00], [4.45, 0.00], [4.61, 0.00], [4.61, 0.00],
8200+
[4.77, 0.00], [4.77, 0.00], [4.93, 0.00], [4.93, 0.00],
8201+
])
8202+
8203+
minx = np.min(verts[:, 0])
8204+
miny = np.min(verts[:, 1])
8205+
maxx = np.max(verts[:, 0])
8206+
maxy = np.max(verts[:, 1])
8207+
8208+
p = mpath.Path(verts)
8209+
8210+
fig, ax = plt.subplots()
8211+
ax.add_patch(mpatches.PathPatch(p))
8212+
ax.autoscale()
8213+
8214+
assert ax.get_xlim()[0] <= minx
8215+
assert ax.get_xlim()[1] >= maxx
8216+
assert ax.get_ylim()[0] <= miny
8217+
assert ax.get_ylim()[1] >= maxy
8218+
8219+
81688220
def test_get_xticklabel():
81698221
fig, ax = plt.subplots()
81708222
ax.plot(np.arange(10))

0 commit comments

Comments
 (0)