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

Skip to content

Commit 800b32c

Browse files
committed
FIX: fix if colorbar is a child
1 parent 95e58d7 commit 800b32c

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

lib/matplotlib/colorbar.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,14 @@ def __init__(self, parent, userax=True):
251251
outer_ax = fig.add_axes(parent.get_position())
252252
# copy the locator if one exists:
253253
outer_ax._axes_locator = parent._axes_locator
254-
# remove the parent from the figure...
255-
parent.remove()
254+
# if the parent is a child of another axes, swap these...
255+
if parent._axes is not None:
256+
if parent in parent._axes.child_axes:
257+
parent._axes.add_child_axes(outer_ax)
258+
outer_ax._axes.child_axes.remove(parent)
259+
else:
260+
# remove the parent from the figure...
261+
parent.remove()
256262
else:
257263
outer_ax = parent
258264

@@ -401,7 +407,7 @@ def __init__(self, ax, cmap=None,
401407
extendfrac=None,
402408
extendrect=False,
403409
label='',
404-
userax=True,
410+
userax=False,
405411
):
406412
_api.check_isinstance([colors.Colormap, None], cmap=cmap)
407413
_api.check_in_list(

lib/matplotlib/tests/test_colorbar.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,3 +743,17 @@ def test_axes_handles_same_functions(fig_ref, fig_test):
743743
caxx.set_yticks(np.arange(20))
744744
caxx.set_yscale('log')
745745
caxx.set_position([0.92, 0.1, 0.02, 0.7])
746+
747+
748+
def test_inset_colorbar_layout():
749+
fig, ax = plt.subplots(constrained_layout=True, figsize=(3, 6))
750+
pc = ax.imshow(np.arange(100).reshape(10, 10))
751+
cax = ax.inset_axes([1.02, 0.1, 0.03, 0.8])
752+
cb = fig.colorbar(pc, cax=cax)
753+
754+
fig.draw_no_output()
755+
# make sure this is in the figure. In the colorbar swapping
756+
# it was being dropped from the list of children...
757+
np.testing.assert_allclose(cb.ax.get_position().bounds,
758+
[0.87, 0.342, 0.0237, 0.315], atol=0.01)
759+
assert cb.ax.outer_ax in ax.child_axes

0 commit comments

Comments
 (0)