Skip to content

Commit

Permalink
fix matplotlib#29410 Modifying Axes' position also alters the origina…
Browse files Browse the repository at this point in the history
…l Bbox object used for initialization (matplotlib#29411)

* fix matplotlib#29410

* Update test_axes_set_position_external_bbox_unchanged to use figure comparison
  • Loading branch information
hu-xiaonan authored Jan 10, 2025
1 parent ca8079b commit 7fcb063
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ def __init__(self, fig,
args = (rect,)
subplotspec = None
if len(args) == 1 and isinstance(args[0], mtransforms.Bbox):
self._position = args[0]
self._position = args[0].frozen()
elif len(args) == 1 and np.iterable(args[0]):
self._position = mtransforms.Bbox.from_bounds(*args[0])
else:
Expand Down
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9610,3 +9610,14 @@ def test_bar_color_precedence():
bars = ax.bar([31, 32, 33], [4, 5, 6], color='red', facecolor='green')
for bar in bars:
assert mcolors.same_color(bar.get_facecolor(), 'green')


@check_figures_equal(extensions=['png'])
def test_axes_set_position_external_bbox_unchanged(fig_test, fig_ref):
# From #29410: Modifying Axes' position also alters the original Bbox
# object used for initialization
bbox = mtransforms.Bbox([[0.0, 0.0], [1.0, 1.0]])
ax_test = fig_test.add_axes(bbox)
ax_test.set_position([0.25, 0.25, 0.5, 0.5])
assert (bbox.x0, bbox.y0, bbox.width, bbox.height) == (0.0, 0.0, 1.0, 1.0)
ax_ref = fig_ref.add_axes([0.25, 0.25, 0.5, 0.5])

0 comments on commit 7fcb063

Please sign in to comment.