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

Skip to content

Commit e04ebd2

Browse files
authored
Merge pull request #14997 from dstansby/fmt-loc-setting
FIX: Correctly set formatters and locators on removed shared axis
2 parents 2cfa1c1 + 24d1837 commit e04ebd2

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

lib/matplotlib/figure.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,25 +1602,37 @@ def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
16021602
return axarr
16031603

16041604
def _remove_ax(self, ax):
1605-
def _reset_loc_form(axis):
1605+
def _reset_locators_and_formatters(axis):
16061606
# Set the formatters and locators to be associated with axis
16071607
# (where previously they may have been associated with another
16081608
# Axis isntance)
1609+
#
1610+
# Because set_major_formatter() etc. force isDefault_* to be False,
1611+
# we have to manually check if the original formatter was a
1612+
# default and manually set isDefault_* if that was the case.
16091613
majfmt = axis.get_major_formatter()
1610-
if not majfmt.axis.isDefault_majfmt:
1611-
axis.set_major_formatter(majfmt)
1614+
isDefault = majfmt.axis.isDefault_majfmt
1615+
axis.set_major_formatter(majfmt)
1616+
if isDefault:
1617+
majfmt.axis.isDefault_majfmt = True
16121618

16131619
majloc = axis.get_major_locator()
1614-
if not majloc.axis.isDefault_majloc:
1615-
axis.set_major_locator(majloc)
1620+
isDefault = majloc.axis.isDefault_majloc
1621+
axis.set_major_locator(majloc)
1622+
if isDefault:
1623+
majloc.axis.isDefault_majloc = True
16161624

16171625
minfmt = axis.get_minor_formatter()
1618-
if not minfmt.axis.isDefault_minfmt:
1619-
axis.set_minor_formatter(minfmt)
1626+
isDefault = majloc.axis.isDefault_minfmt
1627+
axis.set_minor_formatter(minfmt)
1628+
if isDefault:
1629+
minfmt.axis.isDefault_minfmt = True
16201630

16211631
minloc = axis.get_minor_locator()
1622-
if not minfmt.axis.isDefault_minloc:
1623-
axis.set_minor_locator(minloc)
1632+
isDefault = majloc.axis.isDefault_minloc
1633+
axis.set_minor_locator(minloc)
1634+
if isDefault:
1635+
minloc.axis.isDefault_minloc = True
16241636

16251637
def _break_share_link(ax, grouper):
16261638
siblings = grouper.get_siblings(ax)
@@ -1634,11 +1646,11 @@ def _break_share_link(ax, grouper):
16341646
self.delaxes(ax)
16351647
last_ax = _break_share_link(ax, ax._shared_y_axes)
16361648
if last_ax is not None:
1637-
_reset_loc_form(last_ax.yaxis)
1649+
_reset_locators_and_formatters(last_ax.yaxis)
16381650

16391651
last_ax = _break_share_link(ax, ax._shared_x_axes)
16401652
if last_ax is not None:
1641-
_reset_loc_form(last_ax.xaxis)
1653+
_reset_locators_and_formatters(last_ax.xaxis)
16421654

16431655
def clf(self, keep_observers=False):
16441656
"""

lib/matplotlib/tests/test_figure.py

Lines changed: 7 additions & 0 deletions
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)