Skip to content

Commit fe858aa

Browse files
committed
Correct set formatters and locators on removed shared axis
1 parent 0f54425 commit fe858aa

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

lib/matplotlib/figure.py

+23-11
Original file line numberDiff line numberDiff line change
@@ -1596,25 +1596,37 @@ def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
15961596
return axarr
15971597

15981598
def _remove_ax(self, ax):
1599-
def _reset_loc_form(axis):
1599+
def _reset_loc_from(axis):
16001600
# Set the formatters and locators to be associated with axis
16011601
# (where previously they may have been associated with another
16021602
# Axis isntance)
1603+
#
1604+
# Because set_major_formatter() etc. force isDefault_* to be False,
1605+
# we have to manually check if the original formatter was a
1606+
# default and manually set isDefault_* if that was the case.
16031607
majfmt = axis.get_major_formatter()
1604-
if not majfmt.axis.isDefault_majfmt:
1605-
axis.set_major_formatter(majfmt)
1608+
isDefault = majfmt.axis.isDefault_majfmt
1609+
axis.set_major_formatter(majfmt)
1610+
if isDefault:
1611+
majfmt.axis.isDefault_majfmt = True
16061612

16071613
majloc = axis.get_major_locator()
1608-
if not majloc.axis.isDefault_majloc:
1609-
axis.set_major_locator(majloc)
1614+
isDefault = majloc.axis.isDefault_majloc
1615+
axis.set_major_locator(majloc)
1616+
if isDefault:
1617+
majloc.axis.isDefault_majloc = True
16101618

16111619
minfmt = axis.get_minor_formatter()
1612-
if not minfmt.axis.isDefault_minfmt:
1613-
axis.set_minor_formatter(minfmt)
1620+
isDefault = majloc.axis.isDefault_minfmt
1621+
axis.set_minor_formatter(minfmt)
1622+
if isDefault:
1623+
minfmt.axis.isDefault_minfmt = True
16141624

16151625
minloc = axis.get_minor_locator()
1616-
if not minfmt.axis.isDefault_minloc:
1617-
axis.set_minor_locator(minloc)
1626+
isDefault = majloc.axis.isDefault_minloc
1627+
axis.set_minor_locator(minloc)
1628+
if isDefault:
1629+
minloc.axis.isDefault_minloc = True
16181630

16191631
def _break_share_link(ax, grouper):
16201632
siblings = grouper.get_siblings(ax)
@@ -1628,11 +1640,11 @@ def _break_share_link(ax, grouper):
16281640
self.delaxes(ax)
16291641
last_ax = _break_share_link(ax, ax._shared_y_axes)
16301642
if last_ax is not None:
1631-
_reset_loc_form(last_ax.yaxis)
1643+
_reset_loc_from(last_ax.yaxis)
16321644

16331645
last_ax = _break_share_link(ax, ax._shared_x_axes)
16341646
if last_ax is not None:
1635-
_reset_loc_form(last_ax.xaxis)
1647+
_reset_loc_from(last_ax.xaxis)
16361648

16371649
def clf(self, keep_observers=False):
16381650
"""

lib/matplotlib/tests/test_figure.py

+7
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,10 @@ def test_axes_removal():
480480
axs[0].plot([datetime(2000, 1, 1), datetime(2000, 2, 1)], [0, 1])
481481
assert isinstance(axs[0].xaxis.get_major_formatter(),
482482
ScalarFormatter)
483+
484+
485+
def test_removed_axis():
486+
# Simple smoke test to make sure removing a shared axis works
487+
fig, axs = plt.subplots(2, sharex=True)
488+
axs[0].remove()
489+
fig.canvas.draw()

0 commit comments

Comments
 (0)