Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6be5be8

Browse files
authored
Merge pull request #25712 from olinjohnson/fix-fill_between
Prevents axes limits from being resized by axes.fill_between
2 parents f9b59bc + 8c5305f commit 6be5be8

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5431,7 +5431,12 @@ def get_interp_point(idx):
54315431
np.column_stack([ind[where], dep2[where]])])
54325432
if ind_dir == "y":
54335433
pts = pts[:, ::-1]
5434-
self.update_datalim(pts, updatex=True, updatey=True)
5434+
5435+
up_x = up_y = True
5436+
if "transform" in kwargs:
5437+
up_x, up_y = kwargs["transform"].contains_branch_seperately(self.transData)
5438+
self.update_datalim(pts, updatex=up_x, updatey=up_y)
5439+
54355440
self.add_collection(collection, autolim=False)
54365441
self._request_autoscale_view()
54375442
return collection

lib/matplotlib/tests/test_axes.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8572,3 +8572,19 @@ def test_ecdf_invalid():
85728572
plt.ecdf([1, np.nan])
85738573
with pytest.raises(ValueError):
85748574
plt.ecdf(np.ma.array([1, 2], mask=[True, False]))
8575+
8576+
8577+
def test_fill_between_axes_limits():
8578+
fig, ax = plt.subplots()
8579+
x = np.arange(0, 4 * np.pi, 0.01)
8580+
y = 0.1*np.sin(x)
8581+
threshold = 0.075
8582+
ax.plot(x, y, color='black')
8583+
8584+
original_lims = (ax.get_xlim(), ax.get_ylim())
8585+
8586+
ax.axhline(threshold, color='green', lw=2, alpha=0.7)
8587+
ax.fill_between(x, 0, 1, where=y > threshold,
8588+
color='green', alpha=0.5, transform=ax.get_xaxis_transform())
8589+
8590+
assert (ax.get_xlim(), ax.get_ylim()) == original_lims

0 commit comments

Comments
 (0)